title: Socket not sending to Client or Client not recieving
body: <p>I have a Server class and a Client class which is run on an android device. Basically, I need to load the user's friends and their IP address. The user's friends are stored in an ArrayList. So the client must send the friend's name and the server will respond with an IP address if the friend is online or a "-1" if the friend is offline. The server has access to all the users and stores it in an Arraylist of my defined type. My problem is that the client doesn't receive the IP Address/"-1" from the server, so only one friend is checked as the server is waiting for a response. The Login of Client is done elsewhere and is working perfectly.
  Server: </p>
  
  <pre><code>public class Server {
       private static final int port = 9001;
       private static final String IPAddr = "xxx.xxx.xx.10";
       ServerSocket server = null;
       ArrayList &lt;Client&gt; users = new ArrayList&lt;Client&gt;();
  
  
       public Server(){
           readUsers();
  
          try{
              server = new ServerSocket(port);
              System.out.println("connected server on port" + port);
              while(true){
                  System.out.println("waiting for connection my ip add is "+ InetAddress.getLocalHost().getHostAddress());
                  Socket clientsocket = server.accept();
                  System.out.println("Connect to client:"+ clientsocket.getInetAddress().getHostName());
                  ClientThread client = new ClientThread(clientsocket);
                  client.start();
              }
          } catch(IOException e) {
              System.err.println("Could not listen on port");
          }
       }
  
       public void readUsers() {
           //read form user text file on start up 
           ReadFile reader = new ReadFile("users.txt");
          try{
           String[] data = reader.OpenFile();
           for (int i = 0 ; i&lt;data.length; i++){
              String[] parts = data[i].split(" ");
              ArrayList&lt;String&gt; ips = new ArrayList&lt;String&gt;();
               for(int j =2; j&lt;parts.length; j++){
                   ips.add(parts[j]);
               }
  
               Client clnt = new Client(parts[0],parts[1], ips);
                users.add(clnt);
           }
          }catch(Exception e){
              System.err.println("Couldn't read in data");
          }
  
       }
       public void RegisterUser(){
           //append to user text if a new user registers
       }
  
  //Thread     
      public class ClientThread extends Thread {
          private Socket sckt = null;
          private String purpose;
          ObjectOutputStream objectOutput;
          ObjectInputStream objectInput;
  
          public ClientThread(Socket sckt){
              super("ClientThread");
              this.sckt = sckt;
  
          }
          public void run(){
              try{
              objectOutput = new ObjectOutputStream(sckt.getOutputStream());
              objectOutput.flush();
              objectInput = new ObjectInputStream(sckt.getInputStream());
              purpose = objectInput.readUTF();
              if(purpose.contains("login")){
                  LoginUser();
              }else {
                  LoadClientIP();
              }
              } catch(Exception e){
                  System.err.println("Couldnt read purpose: "+ purpose);
              }
          }
          public void LoginUser(){
              try{
                  String Username = objectInput.readUTF();
                  String Password = objectInput.readUTF();
                  int ClientIndex = isClient(Username);
                  if (ClientIndex != -1){
                      if(users.get(ClientIndex).password.equals(Password)){
                          //password correct  -&gt; send friends
                          users.get(ClientIndex).online = true;
                          users.get(ClientIndex).SetCurrentIP(sckt.getRemoteSocketAddress().toString());
                          objectOutput.writeUTF("correct");
                          System.out.println(Username + " is correct");
                          LoadClientFriends(Username, ClientIndex);
  
                          objectOutput.writeObject(users.get(ClientIndex).Friends);
                          System.out.println("Friends sent");
                      } else {
                          //password incorrect -&gt; retry
                          objectOutput.writeUnshared("password");
                          System.out.println(Username + " has wrong password");
  
                      }
                  } else {
                      //not a registered client 
                      objectOutput.writeUTF("wrong");
                      System.out.println(Username + " is not a client");
                  }
              } catch(Exception e){
                  System.err.println("Couldnt connect to Client socket");
              }
          }
          public void LoadClientIP(){
              try{
  
              int size = Integer.parseInt(objectInput.readUTF()); //keep track of the number of friends
              System.out.println("The size of friends:"+ size);
  
              for (int j =0; j&lt;size; j++){
                  String client  = objectInput.readUTF();
                  System.out.println("Client: "+ client);
                  int i = isClient(client);   //index of friend is user arraylist
                  if (users.get(i).online == true){
                      String IP_add = users.get(i).IP.get(users.get(i).CurrentIP);
                      objectOutput.writeUTF(IP_add);
                      System.out.println("Client is at index i "+ i + " with IP "+ IP_add);
                  }else {
                      objectOutput.writeUTF("-1");
                      System.out.println("Client is not online");
                  }
  
              }
              System.out.println("out of while");
              }catch (Exception e){
                  System.err.println("Couldn't load IP");
              }
          }
      }
      public void LoadClientFriends(String name, int index){
          //upon a client signing in, server must load his friends contacts
           ReadFile reader = new ReadFile(name+".txt");
           try{
               String[] data = reader.OpenFile();
  
               for (int i = 0 ; i&lt;data.length; i++){
                    users.get(index).Friends.add(data[i]);
  
               }
              }catch(Exception e){
                  System.err.println("Couldn't read in friend data");
              }
      }
      public int isClient(String name){
          boolean isclient = false;
          int i =0;
          int index =-1;
          while((isclient == false) &amp;&amp; (i&lt;users.size())){
              if (name.equals(users.get(i).Username)){
                  isclient = true;
                  index = i;
              }else {
                  i++;
              }
          }
  
          return index;
      }
      public static void main(String[] args){
          Server svr = new Server();
      }
  }
  </code></pre>
  
  <p>Client/Android:    </p>
  
  <pre><code> public class chat_screen extends ActionBarActivity {
       ArrayList&lt;String&gt; friends;
       ArrayList&lt;String&gt; onlineIP;
       ArrayList&lt;String&gt; onlineFriend;
       private static final int port = 9001;
       private static final String IPAddr = "xx.x.x.4";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.chat_menu);
          friends = (ArrayList&lt;String&gt;) getIntent().getSerializableExtra("Friends");
  
       Thread FriendThread = new Thread(Connect);
       FriendThread.start();
      }
  
      //Thread
     Runnable Connect = new Runnable()
     {
         public void run()
         {
          try {
              //Problems: getting stuck when reading in friends, only is able to send the first friend
              Socket connection = new Socket(IPAddr,port);
  
              ObjectOutputStream objectout = new ObjectOutputStream(connection.getOutputStream());
              objectout.flush();
              ObjectInputStream objectin = new ObjectInputStream(connection.getInputStream()); 
  
              //send purpose
              objectout.writeUTF("load");
              objectout.flush();
              objectout.writeUTF(Integer.toString(friends.size()));
              objectout.flush();
              //send friends name to get IP
              for(int i =0; i&lt;friends.size(); i++){
                  objectout.writeUTF(friends.get(i));
                  objectout.flush();
                  String ip = objectin.readUTF(); //stuck on this line
  
                  if ((ip.contains("-1"))== false){
                      onlineIP.add(ip);
                      onlineFriend.add(friends.get(i));
                  }else {
                     //do nothing
                  }
              }
              connection.close();
          }catch (Exception e){
              finish();
          }
       }
      };
  }
  </code></pre>