title: Client-Server Socket connection works when client socket connects to local IP and does not work with a global IP. (over 4G connection)
body: <p>Hi I am new to programming and here I am trying to create a Client-Server Socket connection using Java on Android Studio, client socket on one Android phone, server socket on another. Code works regardless of the port I use when I connect both phones on the same router, and use internal IP (192.168.1.4) for client socket instead of global IP (183.90.37.22). However, if I were to turn on 4G connection and use global IP in my code, client socket cannot connect to the server socket on the other phone. However when port 80 was used, I managed to see the toasts showing till "Message Sent" <strong>ALTHOUGH</strong> no message/toasts showed in the phone coded with the server socket. I very much appreciate all the help I can get and it would be great if a thorough explanation on how I should assign TCP ports to client &amp; server sockets could be given! =)</p>
  
  <p>Client Socket is presented first:</p>
  
  <pre><code>Button button = findViewById(R.id.button);
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              MessageSenderThread messageSender = new MessageSenderThread("messageSender");
              messageSender.start();
          }
      });
  
  }
  
  class MessageSenderThread extends Thread {
      private String threadName;
      private Thread t;
  
      MessageSenderThread(String s) {
          threadName = s;
      }
  
      @Override
      public void run() {
          try {
              showToast("Connecting to server...");
              Socket socket = new Socket("183.90.37.22", 80);
              showToast("Connection established");
              OutputStream outputStream = socket.getOutputStream();
              OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
              PrintWriter printWriter = new PrintWriter(outputStreamWriter);
              printWriter.println("MESSAGE");
              printWriter.flush();
              showToast("Message sent");
  
              outputStream.close();
              outputStreamWriter.close();
              printWriter.close();
              socket.close();
              showToast("Socket closed");
          }
          catch (IOException e) {
              showToast("Connection FAILED");
          }
      }
  
      @Override
      public void start() {
          if (t == null) {
              t = new Thread(this, threadName);
              t.start();
          }
      }
  }
  </code></pre>
  
  <p>Server side of the code is presented below:</p>
  
  <pre><code>MessageReceiverThread messageReceiverThread = new MessageReceiverThread("messageReceiver");
  messageReceiverThread.start();
  
  class MessageReceiverThread extends Thread {
      private String threadName;
      private Thread t;
  
      MessageReceiverThread(String s) {
          threadName = s;
      }
  
      @Override
      public void run() {
          try {
              while (true) {
                  ServerSocket serverSocket = new ServerSocket(5000);
                  showToast("Server is listening...");
                  Socket socket = serverSocket.accept();
                  showToast("Connection established");
                  InputStream inputStream = socket.getInputStream();
                  InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                  String message = bufferedReader.readLine();
  
                  showToast(message);
                  inputStream.close();
                  inputStreamReader.close();
                  bufferedReader.close();
                  socket.close();
                  serverSocket.close();
              }
  
          } catch (IOException e) {
              showToast("Connection FAILED");
          }
      }
  </code></pre>