Пример #1
0
  // Connect to server
  private void connectToServer() throws IOException {

    showMessage("Attempting connection...\n");

    try {
      do {
        try {
          username = JOptionPane.showInputDialog("Enter a username");
          obj.put("username", username);
        } catch (JSONException e) {
          e.printStackTrace();
        }
      } while (obj.getString("username").contains(" ") || obj.getString("username") == null);
    } catch (HeadlessException | JSONException e) {
      e.printStackTrace();
    }

    connection =
        new Socket(
            InetAddress.getByName(serverIP), 6789); // The IP address of the server and the port
    showMessage("Connected to: " + connection.getInetAddress().getHostName());
  }