title: How to create a Global Client Socket in my Android Application?
body: <p>Hello dear programmers ,</p>
  
  <p>I am trying to make a tic tac toe game using android, my android application contains several activities, one of these activities can  the allows client to send a message to the server asking if X user wants to challenge, if the user accepts the challenge the server messages me and we both move forward to another activity.</p>
  
  <p><strong>My server is running as a regular java code on my PC, this is my server code :</strong></p>
  
  <pre><code>public class Server {
  
      ServerSocket serverSocket;
      ArrayList&lt;ServerThread&gt; allClients = new ArrayList&lt;ServerThread&gt;();
  
      public static void main(String[] args) {
          new Server();
  
      }
  
      public Server() {
          // ServerSocket is only opened once !!!
  
          try {
              serverSocket = new ServerSocket(6000);
  
              System.out.println("Waiting on port 6000...");
              boolean connected = true;
  
              // this method will block until a client will call me
              while (connected) {
                  Socket singleClient = serverSocket.accept();
                  // add to the list
                  ServerThread myThread = new ServerThread(singleClient);
                  allClients.add(myThread);
                  myThread.start();
              }
  
              // here we also close the main server socket
              serverSocket.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      class ServerThread extends Thread {
          Socket threadSocket;
          String userName;
          boolean isClientConnected;
          InputStream input;
          ObjectInputStream ois;
          OutputStream output;
          ObjectOutputStream oos; // ObjectOutputStream
  
          public ServerThread(Socket s) {
              threadSocket = s;
          }
  
          public void sendText(String text) {
              try {
                  oos.writeObject(text);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
          public void run() {
  
              try {
                  input = threadSocket.getInputStream();
                  ois = new ObjectInputStream(input);
                  output = threadSocket.getOutputStream();
                  oos = new ObjectOutputStream(output);
  
                  userName = (String) ois.readObject();
                  isClientConnected = true;
                  System.out.println("User " + userName + " has connected");
  
                  while (isClientConnected) {
                      String singleText = (String) ois.readObject();
                      System.out.println(singleText);
  
                      for (ServerThread t : allClients)
                          t.sendText(singleText);
                      // oos.writeObject(singleText);
                  }
                  // close all resources (streams and sockets)
                  ois.close();
                  oos.close();
                  threadSocket.close();
  
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
  }    
  </code></pre>
  
  <p><strong>I use the communication between clients in only two activies, both activites contain the same connectUser()  code :</strong></p>
  
  <pre><code>public class MenuActivity extends Activity {
  
      public static final String HOST = "10.0.2.2";
      public static final int PORT = 6000;
  
      static ConnectThread clientThread;
      boolean isConnected;
  
      static boolean isOnline = false;
  
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_menu);
  
          runOnUiThread(new Runnable() {
              public void run() {
                  connectUser();
  
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
      public void connectUser() {
          clientThread = new ConnectThread();
          clientThread.start();
  
  
      }
  
      class ConnectThread extends Thread {
          InputStream input;
          OutputStream output;
          ObjectOutputStream oos;
          Socket s;
  
          public void sendText(String text) {
              try {
                  oos.writeObject(text);
                  System.out.println(text);
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
          public void run() {
              try {
                  s = new Socket(HOST, PORT);
                  output = s.getOutputStream();
                  oos = new ObjectOutputStream(output);
                  oos.writeObject(un);
  
                  isOnline = true;
                  isConnected = true;
  
                  new ListenThread(s).start();
  
              } catch (UnknownHostException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  
      class ListenThread extends Thread {
  
          Socket s;
          InputStream input;
          ObjectInputStream ois;
  
          public ListenThread(Socket s) {
              this.s = s;
              try {
                  input = s.getInputStream();
                  ois = new ObjectInputStream(input);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
  
          public void run() {
              while (isConnected) {
                  try {
                      final String inputMessage = (String) ois.readObject();
                  //do something with the message                 }
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>I use this code this code to send message to the server :</strong></p>
  
  <pre><code>                clientThread.sendText(user + " " + opponent + " play");
  </code></pre>
  
  <hr>
  
  <p><strong><em>The problem</em></strong> is that when I create the connection at the first activity, then move to the second activity I create another connection , which means so far I am having two connections, same with other clients and then the server seems to return a timed out error.</p>
  
  <p><strong><em>My question</em></strong> is how to do a global client variable that is created once and can be used in each activity. I saw many suggestions like socket service or asyntask , but I need more direction and help</p>
  
  <p>Thanks in advance.</p>