title: Android - Server Socket
body: <p>I have few question regarding Socket Communication in Android.</p>
  
  <p>1)I have developed server socket app for android that listens to port 8888.</p>
  
  <p>a) When i host my server on the emulator I'm unable to communicate to it through Client   application that I have on my PC since both (Emulator &amp; Client) app are on my laptop &amp; on the same network i think that they should be able to communicate with each other.</p>
  
  <p>b) When i deploy the same server app on my android mobile device and try to communicate it through the same Client Application that I have on my PC, the client application gives a timeout exception as its unable to communicate to it.</p>
  
  <p>My first question is How can i test server/client socket app with Emulator &amp; 1 android device? Can i even use my PC's client socket application to test my server socket?</p>
  
  <p>**I have Client Socket Application for my other application so theres no problem with the client application.</p>
  
  <p>2) My second question is to test my server app on the android device do I have to forward the desired port?
     a) For Emulator: How can I forward port?
     b) For Device: How can i forward port?
     c) Can i forward port programmatically?</p>
  
  <p>**Just for Information:</p>
  
  <p>I'm using Eclipse as android developement tool.</p>
  
  <p>** MY Server Code as there can also be problem with my server socket code too.</p>
  
  <pre><code>Socket socket = null;
              DataInputStream dataInputStream = null;
              DataOutputStream dataOutputStream = null;
              ServerSocket serverSocket = null;
          try
          {  
              serverSocket = new ServerSocket(SERVERPORT);
              System.out.println("Listening :" + SERVERPORT);
              System.out.println("Server IP:" + SERVERIP);
          }
          catch (Exception e) 
          {               
              e.printStackTrace();
          }
          while(true)
          {
              try
              {
                  socket = serverSocket.accept();
                  BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  System.out.println("ip: " + socket.getInetAddress());                   
                  String str = in.readLine();
                  System.out.println("message Received: " + str);
              } 
              catch (Exception e) 
              {                   
                  e.printStackTrace();
              }               
              finally
              {
                  if( socket!= null)
                  {
                      try 
                      {
                          socket.close();
                      }
                      catch (IOException e) 
                      {
                          e.printStackTrace();
                      }
                  }
                  if( dataInputStream!= null)
                  {
                      try
                      {
                          dataInputStream.close();
                      }
                      catch (IOException e) 
                      {
                          e.printStackTrace();
                      }
                  }                   
                  if( dataOutputStream!= null)
                  {
                      try 
                      {
                          dataOutputStream.close();
                      }
                      catch (IOException e) 
                      {
                          e.printStackTrace();
                      }
                  }
              }
          }
  </code></pre>
  
  <p><strong>Edited:</strong>
  A very interesting part is that if i set port to 8080 PC's Client Socket App do connect to Android App on my android device but i don't receive socket on my server nor the data I send. Nothing happens after the link => <strong>socket = serversocket.accept();</strong></p>
  
  <p>Also I have set the permission in the manifest.</p>
  
  <p>
  <br>
  </p>