Search in sources :

Example 1 with SocketHints

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ServerSocketHints(com.badlogic.gdx.net.ServerSocketHints) ServerSocket(com.badlogic.gdx.net.ServerSocket) IOException(java.io.IOException) ServerSocketHints(com.badlogic.gdx.net.ServerSocketHints) SocketHints(com.badlogic.gdx.net.SocketHints) ServerSocket(com.badlogic.gdx.net.ServerSocket) Socket(com.badlogic.gdx.net.Socket)

Example 2 with SocketHints

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);
}
Also used : Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Message(com.janfic.games.computercombat.network.Message) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) JsonValue(com.badlogic.gdx.utils.JsonValue) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) JsonReader(com.badlogic.gdx.utils.JsonReader) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) ArrayList(java.util.ArrayList) List(java.util.List) ServerAPI(com.janfic.games.computercombat.network.client.ServerAPI) SocketHints(com.badlogic.gdx.net.SocketHints) ProgressBar(com.badlogic.gdx.scenes.scene2d.ui.ProgressBar) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

SocketHints (com.badlogic.gdx.net.SocketHints)2 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 ServerSocket (com.badlogic.gdx.net.ServerSocket)1 ServerSocketHints (com.badlogic.gdx.net.ServerSocketHints)1 Socket (com.badlogic.gdx.net.Socket)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 ProgressBar (com.badlogic.gdx.scenes.scene2d.ui.ProgressBar)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)1 JsonReader (com.badlogic.gdx.utils.JsonReader)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 Message (com.janfic.games.computercombat.network.Message)1 ServerAPI (com.janfic.games.computercombat.network.client.ServerAPI)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1