title: Java TCP send and receive multiple messages between client & server side on same socket
body: <p>I have the following situation,</p>
  
  <ol>
  <li>TCP server will accept the connection fron client</li>
  <li>Client wil send the first request and server will responds to that request and server must wait on the same socket to receive next request from the same client</li>
  <li>Please see the code that i did, With this code server not able to receive the second request send by server and client receiving the first response from server in 2nd receive also.</li>
  <li>Please suggest on this, what is problem in code.</li>
  <li>I try to simulate this case, if anyone met it previously, please kindly suggest on this soon.</li>
  </ol>
  
  <p>Client_MultipleMessages.java:    </p>
  
  <pre><code>public class Client_MultipleMessages {
      public static void main(String[] args) {
          Socket clientSocket = null;
          SocketAddress sockaddr = null;
          boolean IsSocketCreated = false;
          String p_Response = "";
          OutputStream outToServer = null;
          InputStream in = null;
          String strRequestString = "";
          try{
              clientSocket = new Socket();
              sockaddr = new InetSocketAddress("192.168.121.121", 1234);
              try{
                  clientSocket.connect(sockaddr, 1000);
                  if (clientSocket.isConnected()){
                      IsSocketCreated = true;
                  }
              }catch(Exception e){
                  System.out.println("Exception while creating socket,Reason is:"+ e.getMessage());
              }
              int index = 1;
              String req = "REGISTRATION_REQUEST";
              while(index &lt;= 2){
                  if(clientSocket.isConnected()){
                      outToServer = clientSocket.getOutputStream();
                      System.out.println("Request "+index+":"+req);
                      outToServer.write(req.getBytes());
                      outToServer.flush();
                      //clientSocket.setSoTimeout(1000);
                      in = clientSocket.getInputStream();
                      int i = -1;
                      while((i = in.read()) &gt; 0){
                          p_Response += (char) i;
                      }
                      System.out.println("Response "+index+":"+p_Response);
                  }
                  index++;
                  req = "LERGD_ALLOCATE_MSISDN";
              }   
          }catch(Exception ex){
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Server_MultipleMessages.java</p>
  
  <pre><code>public class Server_MultipleMessages {
      public static void main(String[] args) {
          try{
              ServerSocket Server = new ServerSocket (1234);
              while(true){
                  Socket socket = Server.accept();
                  String fromclient = "";
                  BufferedReader inFromClient = null;
                  PrintWriter outToClient = null;
                  String strresponse = "";
                  try{
                      int reqCount = 1;
                      socket.setSoTimeout(2000);
                      while(reqCount &lt;= 2){
                          System.out.println("Request-"+reqCount);
                          inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                          outToClient = new PrintWriter(socket.getOutputStream(),true);
                          char data[] = new char[1200];
                          inFromClient.read(data);
                          for (int i = 0; i &lt; data.length; i++) {
                              fromclient = fromclient + Character.toString(data[i]);
                          }
                          System.out.println("XML Request is from client: "+fromclient+"\n\n");
                          String returnDesc = "success";
                          if(fromclient.contains("REGISTRATION_REQUEST")){
                              System.out.println("Request if for Registeration !!");
                              strresponse = "&lt;REGISTRATION_RESPONSE&gt;&lt;HEADER&gt;&lt;ERROR_CODE&gt;" + 0 + "&lt;/ERROR_CODE&gt;              &lt;ERROR_DESC&gt;" + returnDesc + "&lt;/ERROR_DESC&gt;&lt;/HEADER&gt;&lt;BODY&gt;,DIAMETER-S6A,TCAP,&lt;/BODY&gt;&lt;/REGISTRATION_RESPONSE&gt;";
                      }else if(fromclient.contains("LERGD_ALLOCATE_MSISDN")){
                          System.out.println("Request is for allocate Msisdnm !!");
                          strresponse = "&lt;RESPONSE&gt;&lt;HEADER&gt;&lt;TRANSACTION_ID&gt;123456&lt;/TRANSACTION_ID&gt;&lt;REQUEST_TYPE&gt;LERGD_ALLOCATE_MSISDN&lt;/REQUEST_TYPE&gt;&lt;ERROR_CODE&gt;" + 0 + "&lt;/ERROR_CODE&gt;&lt;ERROR_DESC&gt;" + returnDesc + "&lt;/ERROR_DESC&gt;&lt;/HEADER&gt;&lt;BODY&gt;&lt;ACTION_TAKEN&gt;B&lt;/ACTION_TAKEN&gt;&lt;ALLOCATED_MSISDN&gt;7525600000&lt;/ALLOCATED_MSISDN&gt;&lt;/BODY&gt;&lt;/RESPONSE&gt;";
                      }else{
                          System.out.println("Invalid Request from client !!");
                      }
                      System.out.println("XML Response to be send to client: "+strresponse+"\n\n");
                      outToClient.print(strresponse);
                      outToClient.flush();
                      strresponse = "";
                      fromclient = "";
                      reqCount++;
                  }
              }catch(Exception ex){
                  ex.printStackTrace();
              }finally{
                  if(!socket.isClosed()){
                      socket.close();
                  }
              }
          }
      }catch(Exception ex){
          System.out.println("Error in ProcessXmlRequest : "+ex.getMessage());
      }
  }}
  </code></pre>
  
  <p>Server side output:</p>
  
  <pre><code>Request-1
  XML Request is from client: REGISTRATION_REQUEST
  Request if for Registeration !!
  XML Response to be send to client: &lt;REGISTRATION_RESPONSE&gt;&lt;HEADER&gt;&lt;ERROR_CODE&gt;0&lt;/ERROR_CODE&gt;&lt;ERROR_DESC&gt;success&lt;/ERROR_DESC&gt;&lt;/HEADER&gt;&lt;BODY&gt;,DIAMETER-S6A,TCAP,&lt;/BODY&gt;&lt;/REGISTRATION_RESPONSE&gt;
  Request-2
  java.net.SocketTimeoutException: Read timed out
  </code></pre>
  
  <p>Client side output:</p>
  
  <pre><code>Request 1:REGISTRATION_REQUEST
  Response 1:&lt;REGISTRATION_RESPONSE&gt;&lt;HEADER&gt;&lt;ERROR_CODE&gt;0&lt;/ERROR_CODE&gt;&lt;ERROR_DESC&gt;success&lt;/ERROR_DESC&gt;&lt;/HEADER&gt;&lt;BODY&gt;,DIAMETER-S6A,TCAP,&lt;/BODY&gt;&lt;/REGISTRATION_RESPONSE&gt;
  Request 2:LERGD_ALLOCATE_MSISDN
  Response 2:&lt;REGISTRATION_RESPONSE&gt;&lt;HEADER&gt;&lt;ERROR_CODE&gt;0&lt;/ERROR_CODE&gt;&lt;ERROR_DESC&gt;success&lt;/ERROR_DESC&gt;&lt;/HEADER&gt;&lt;BODY&gt;,DIAMETER-S6A,TCAP,&lt;/BODY&gt;&lt;/REGISTRATION_RESPONSE&gt;
  </code></pre>