title: Reconnecting sockets in Android
body: <p>Here's the problem. I'm attempting to set up a very simple app that will send messages to a Java server.</p>
  
  <p>Here is my server code:</p>
  
  <pre><code>ServerSocket serverSocket = null;
  Socket clientSocket =null;
  try 
  {
      serverSocket = new ServerSocket(4444);
      System.out.println("Server Started...");
      clientSocket = serverSocket.accept();            
  }catch(Exception e){};
  
  Scanner in = new Scanner(clientSocket.getInputStream());
  String inputLine;
  
  while (true) 
  {
      if(in.hasNext())
      {                   
          inputLine=in.nextLine();
          System.out.println("Message from server: "+inputLine);
      }
  }
  </code></pre>
  
  <p>And here's the client code:</p>
  
  <pre><code>Socket client;
  PrintWriter printwriter;
  String serverIpAddress="192.168.173.1";
  Button button;
  TextView text;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button =(Button) findViewById(R.id.button);
      text=(TextView) findViewById(R.id.textView);
  
      button.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
              new Thread(new Runnable()
              {
                  @Override
                  public void run()
                  {
                      try
                      {
                          client = new Socket(serverIpAddress,4444);
                          printwriter = new PrintWriter(client.getOutputStream());
                          printwriter.write("Start");
                          printwriter.flush();
                          //printwriter.close();
                          //client.close();                                   
                      }
                      catch(UnknownHostException e)
                      {
  
                      }
                      catch(IOException e)
                      {
                          e.printStackTrace();
                      }
                  }                           
              }).start();
          }
      });
  }
  </code></pre>
  
  <p>I have one button, when I press it a message is sent to the server and it displays fine, but when I press the button the socket does not reconnect so I can't send the massage again.</p>
  
  <p>To be clear I intend to eventually add more buttons to send different messages. I looked into creating a global socket but that didn't seem to go anywhere.</p>
  
  <p>Any help is greatly appreciated.</p>