Beispiel #1
0
  public void die() {
    running = false;

    if (socket != null) {
      socket.dispose();
    }
  }
  @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);
    }
  }
Beispiel #3
0
  public void run() {
    if (socketType == SocketType.SERVER_SOCKET) {
      connectToTheServer();
    } else if (socketType == SocketType.PLAYER_SOCKET) {
      // The socket is already connected to a player
      connectionState = ConnectionState.CONNECTED;
    }

    if (socket != null) {
      if (socket.isConnected()) {
        listenNetworkMessagesUntilDisconnected();
      }
    }

    handleDisconnection();
  }
Beispiel #4
0
  private void listenNetworkMessagesUntilDisconnected() {
    if (socketType == SocketType.SERVER_SOCKET) {
      Gdx.app.debug(TAG, "Listening messages from the server.");
    } else if (socketType == SocketType.PLAYER_SOCKET) {
      Gdx.app.debug(TAG, "Listening messages from the player.");
    }

    InputStreamReader inputStream = new InputStreamReader(socket.getInputStream());
    char[] readCharacter = new char[1]; // Read character will be stored in this array
    StringBuilder constructMessage = new StringBuilder();

    while (running) {
      try {
        // Read one character from buffer or wait until there is a message in the buffer
        if (inputStream.read(readCharacter) == -1) {
          // Socket disconnected.
          running = false;
        } else {
          constructMessage.append(readCharacter[0]);

          // Message is too long?
          if (constructMessage.length()
              > NetworkManager.getInstance().NETWORK_MESSAGE_MAX_LENGTH_CHARACTERS) {
            constructMessage = new StringBuilder();
            Gdx.app.debug(TAG, "WARNING: Network message was too long and was rejected!");
          }

          if (readCharacter[0] == '>') { // End of the message reached, handle message
            if (socketType == SocketType.SERVER_SOCKET) {
              Gdx.app.debug(TAG, "Got message from the server: " + constructMessage);
            } else if (socketType == SocketType.PLAYER_SOCKET) {
              Gdx.app.debug(
                  TAG,
                  "Got message from the player " + player.getNumber() + ": " + constructMessage);
            }

            RTSProtocolManager.getInstance()
                .handleNetworkMessage(constructMessage.toString(), this);
            constructMessage = new StringBuilder();
          }
        }
      } catch (Exception e) {
        Gdx.app.debug(TAG, "ERROR: while reading buffer: " + e.getMessage());
        running = false;
      }
    }
  }
Beispiel #5
0
  public void sendMessage(final String message) {
    try {
      if (socketType == SocketType.SERVER_SOCKET) {
        Gdx.app.debug(
            TAG,
            "Player "
                + GameMain.getInstance().getPlayer().getNumber()
                + ": Sending message to the server: "
                + message);
      } else if (socketType == SocketType.PLAYER_SOCKET) {
        Gdx.app.debug(
            TAG, "Server: Sending message to the player " + player.getNumber() + ": " + message);
      }

      socket.getOutputStream().write(message.getBytes());
      socket.getOutputStream().flush();
    } catch (Exception e) {
      if (socketType == SocketType.SERVER_SOCKET) {
        Gdx.app.debug(TAG, "WARNING: Unable to send message to server: " + e.getMessage());
      } else if (socketType == SocketType.PLAYER_SOCKET) {
        Gdx.app.debug(TAG, "WARNING: Unable to send message to player: " + e.getMessage());
      }
    }
  }