title: Connect android phone to server socket using Java
body: <p>I'm writing Java code in Eclipse to create an Android browser that communicates with a server via a socket when the browser's Go button is pressed. I've seen several examples and still don't know what is wrong with my code. I have the correct IP address and I am using the same port number in the server and client side of the code.  Here is the client code minus the import statements:</p>
  
  <pre><code>public class MainActivity3 extends Activity {
  
      EditText URLText;
  
      Button GoButton;
      WebView Browser;
      int SERVERPORT = 33333;
      String servernum = "10.215.23.109";
      private boolean connected = true;
  
      private class MyWebViewClient extends WebViewClient {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              view.loadUrl(url);
              return true;
          }
      }
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_activity3);
  
          URLText = (EditText) findViewById(R.id.URL);
          GoButton = (Button) findViewById(R.id.Go);
          Browser = (WebView) findViewById(R.id.WebEngine);
          Browser.loadUrl("http://www.google.co.in/");
  
          GoButton.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
  
                  Browser.setWebViewClient(new MyWebViewClient());
  
                  Thread cThread = new Thread(new ClientThread());
                  cThread.start();
  
                  Browser.loadUrl("http://" + URLText.getText().toString());
  
              }
          });
      }
  
      public class ClientThread implements Runnable {
          @Override
          public void run() {
  
              try {
  
                  Log.d("ClientActivity", "C: Connecting...");
                  Socket socket = new Socket(servernum, SERVERPORT);
              } catch (UnknownHostException e) {
                  System.err.println(e);
              } catch (IOException e) {
                  System.err.println(e);
              }
          }
  
          public boolean onCreateOptionsMenu(Menu menu) {
              getMenuInflater().inflate(R.menu.activity_main_activity3, menu);
              return true;
          }
      }
  }
  </code></pre>
  
  <p>This is the server code:</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      int port = 33334;
      ServerSocket serverSocket = new ServerSocket(port);
      System.out.println("Server socket created at port # "+ port );
      Socket socket = serverSocket.accept();
      System.out.println("Socket accepted");
      System.out.println("Output created");
  
      socket.close();
      serverSocket.close();  
  }
  </code></pre>
  
  <p>This is basically a test code to make sure the phone will connect to the server.  When I run it on my Linux terminal, it only outputs "Server socket created at port # 33334" and waits for the phone to connect with the port which never happens, even after I press go on the browser repeatedly, which leads me to believe there is something wrong with the client side of my code.</p>
  
  <p>Can anyone help me solve this issue?</p>