title: Java socket - the server response is always null
body: <p>I have to connect with a server (I don´t have access to the server code) but the transmission protocol (Socket) is:</p>
  
  <blockquote>
    <p>(client) --> data </p>
    
    <p>ack &lt;-- (server)<br>
     data response &lt;-- (server)</p>
    
    <p>(client) --> ack</p>
  </blockquote>
  
  <p>It's assumed that the server should always respond quickly. I connect to the server, I send the data but the response is NULL and if I debug my code, an exception occurs when I catch the response:
  "java.net.SocketException: Software caused connection abort: recv failed"</p>
  
  <p>My code:</p>
  
  <pre><code>public static void main(String[] args){
      try{
          String order = "datahere";
          String responseServer;   
          BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));  
  
          Socket clientSocket = new Socket();
          InetSocketAddress sa = new InetSocketAddress("XXX.XX.XX.XX", 9300);
          clientSocket.connect(sa,500);
  
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());   
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));    
          outToServer.writeBytes(order);   
          responseServer = inFromServer.readLine();//exception if I try to debug my code   
          System.out.println("From server: " + responseServer); //responseServer is NULL
          clientSocket.close(); 
  
      } catch (IOException ex) {
              System.out.println("Error: "+ex);
      }
  
  }
  </code></pre>
  
  <p>That's wrong? Any idea?</p>
  
  <p>I tried to disable the firewall and also add a rule for the port 9300 but the result is the same.</p>
  
  <p>The client gave me an example code in Vb.Net that it's supposed to work and I try to replicate it in Java.</p>
  
  <p>Code in Vb.Net:</p>
  
  <pre><code>Dim message As String = "datahere";
  
   Try
       Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
  
       Dim client As New TcpClient(ip, port)
       Dim stream As NetworkStream = client.GetStream()
       stream.Write(data, 0, data.Length)
  
       data = New [Byte](2048) {}
  
       Dim responseData As [String] = [String].Empty
       Dim bytes As Integer = stream.Read(data, 0, data.Length)
       responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
  
       stream.Close()
       client.Close()
  
   Catch ex As Exception
  
  End Try
  </code></pre>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>Socket clientSocket = new Socket();
  InetSocketAddress sa = new InetSocketAddress("XXX.XX.XX.XX", 9300);
  clientSocket.connect(sa,500);
  
  clientSocket.getOutputStream().write(order.getBytes("ASCII"));
  
  byte[] data = new byte[2048];
  int bytes = clientSocket.getInputStream().read(data, 0, data.length);
  String responseData = new String(data, 0, bytes, "ASCII");
  System.out.println("From server: " + responseData);
  
  //Another way to catch the response:
  //InputStreamReader in = new InputStreamReader(clientSocket.getInputStream());
  //int data1 = in.read();
  //while(data1 != -1) {
  //   System.out.print((char) data1);
  //   data1 = in.read();
  //}
  
  clientSocket.close();
  </code></pre>