use of com.badlogic.gdx.net.SocketHints in project libgdx by libgdx.
the class PingPongSocketExample method create.
@Override
public void create() {
// setup a server thread where we wait for incoming connections
// to the server
new Thread(new Runnable() {
@Override
public void run() {
ServerSocketHints hints = new ServerSocketHints();
ServerSocket server = Gdx.net.newServerSocket(Protocol.TCP, "localhost", 9999, hints);
// wait for the next client connection
Socket client = server.accept(null);
// read message and send it back
try {
String message = new BufferedReader(new InputStreamReader(client.getInputStream())).readLine();
Gdx.app.log("PingPongSocketExample", "got client message: " + message);
client.getOutputStream().write("PONG\n".getBytes());
} catch (IOException e) {
Gdx.app.log("PingPongSocketExample", "an error occured", e);
}
}
}).start();
// create the client send a message, then wait for the
// server to reply
SocketHints hints = new SocketHints();
Socket client = Gdx.net.newClientSocket(Protocol.TCP, "localhost", 9999, hints);
try {
client.getOutputStream().write("PING\n".getBytes());
String response = new BufferedReader(new InputStreamReader(client.getInputStream())).readLine();
Gdx.app.log("PingPongSocketExample", "got server message: " + response);
} catch (IOException e) {
Gdx.app.log("PingPongSocketExample", "an error occured", e);
}
}
use of com.badlogic.gdx.net.SocketHints in project computercombat by janfic.
the class LoadingScreen method show.
@Override
public void show() {
this.skin = new Skin(Gdx.files.internal(Assets.SKIN));
this.camera = new OrthographicCamera(1920 / 4, 1080 / 4);
this.stage = ComputerCombatGame.makeNewStage(camera);
Pixmap cursor = new Pixmap(Gdx.files.internal(Assets.CURSOR));
Gdx.graphics.setCursor(Gdx.graphics.newCursor(cursor, 0, 0));
this.progressBar = new ProgressBar(0, 1, 0.01f, false, skin.get("default-horizontal", ProgressBarStyle.class));
this.statusLabel = new Label("Loading Assets...", skin);
Table table = new Table(skin);
table.setFillParent(true);
table.add(statusLabel).row();
table.add(progressBar);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int index = 0;
int tries = 0;
JsonReader json = new JsonReader();
JsonValue parsed = json.parse(Gdx.files.internal("connections.json"));
List<String> connections = new ArrayList<>();
for (JsonValue jsonValue : parsed.child) {
connections.add(jsonValue.getString("ip") + " " + jsonValue.getString("port"));
}
System.out.println(connections);
boolean connected = false;
while (connected == false) {
try {
String[] connection = connections.get(index).split(" ");
game.setServerAPI(new ServerAPI(Gdx.net.newClientSocket(Net.Protocol.TCP, connection[0], Integer.parseInt(connection[1]), new SocketHints())));
connected = true;
Thread.sleep(5);
} catch (Exception e) {
tries++;
statusLabel.setText("Failed to Connect to server. Trying again. (" + tries + ")");
if (tries > 4) {
statusLabel.setText("Trying a different server...");
tries = 0;
index++;
index = index % connections.size();
}
}
}
game.getServerAPI().sendMessage(new Message(Type.CONNECTION_REQUEST, "CONNECTION_REQUEST"));
}
});
thread.start();
stage.addActor(table);
}
Aggregations