title: Server/Client Android to PC communication issue
body: <p>Im trying to write a program using sockets having an android phone as the client and my PC as the server i found and slightly modified code for both sides and my server code works but i can't get the client activity to and i don't know acutally how to get the to programs to talk i want to basically be able to have either side input text and have it sent and set visable to the other i know how much of a stickler you guys are for questions so... How do i get these to communicate? </p>
  
  <p>Client code (The emulator crashes before i can even begin to see the problem)</p>
  
  <pre><code>package com.example.Socket;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.view.View;
  import android.view.View.*;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import java.io.*;
  import java.net.*;
  
  
  public class ClientActivity extends Activity {
  
      private EditText serverIp;
  
      private Button connectPhones;
  
      private String serverIpAddress = "";
  
      private boolean connected = false;
  
      private Handler handler = new Handler();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.client);
  
          serverIp = (EditText) findViewById(R.id.server_ip);
          connectPhones = (Button) findViewById(R.id.connect_phones);
          connectPhones.setOnClickListener((android.view.View.OnClickListener) connectListener);
      }
  
      private OnClickListener connectListener = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              if (!connected) {
                  serverIpAddress = serverIp.getText().toString();
                  if (!serverIpAddress.equals("")) {
                      Thread cThread = new Thread(new ClientThread());
                      cThread.start();
                  }
              }
          }
      };
  
      public class ClientThread implements Runnable {
  
          public void run() {
              try {
                  InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                  Log.d("ClientActivity", "C: Connecting...");
                  Socket socket = new Socket(serverAddr, 9999);
                  connected = true;
                  while (connected) {
                      try {
                          Log.d("ClientActivity", "C: Sending command.");
                          PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                      .getOutputStream())), true);
                              // where you issue the commands
                              out.println("Hey Server!");
                              Log.d("ClientActivity", "C: Sent.");
                      } catch (Exception e) {
                          Log.e("ClientActivity", "S: Error", e);
                      }
                  }
                  socket.close();
                  Log.d("ClientActivity", "C: Closed.");
              } catch (Exception e) {
                  Log.e("ClientActivity", "C: Error", e);
                  connected = false;
              }
          }
      }
  }   
  </code></pre>
  
  <p>the Server code</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  /**
   * Simple server using Java Sockets.
   * @author Jonathan Engelsma (http://www.cis.gvsu.edu/~engelsma)
   *
   */
  public class Server {
  
  /**
   * @param args
   */
  public static void main(String[] args) {
  
      try {
          // First we create a server socket and bind it to port 9999.
          ServerSocket myServerSocket = new ServerSocket(9999);
  
  
          // wait for an incoming connection... 
          System.out.println("Server is waiting for an incoming connection on host=" 
                  + InetAddress.getLocalHost().getCanonicalHostName() 
                  + " port=" + myServerSocket.getLocalPort());
          Socket skt = myServerSocket.accept();
  
          // ok, got a connection.  Let's use java.io.* niceties to read and write from the connection.
          BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
          PrintStream myOutput = new PrintStream(skt.getOutputStream());  
  
          // attempt to read input from the stream.
          String buf = myInput.readLine();
  
          // if we got input, print it out and write a message back to the remote client..
          if (buf != null) {
              System.out.println("Server read: [" + buf + "]");
              myOutput.print("got it");
          }
  
          // close the connection.
          skt.close();
          System.out.println("Server is exiting");
  
      } catch (IOException ex) {
          ex.printStackTrace();
          System.out.println("Whoops, something bad happened!  I'm outta here.");
      }
  
  
  
  }
  </code></pre>
  
  <p>}</p>