title: java.net.SocketException: sendto failed: EPIPE (Broken pipe) on Android
body: <p>I am trying to read data from server socket from client in Android. </p>
  
  <p>Here are the snippets of code that I'm using:</p>
  
  <p><strong>Client</strong> (On Android in JAVA)</p>
  
  <pre><code>    DataOutputStream dataOutputStream = null;
      DataInputStream dataInputStream = null;
      try {
  
          if (client.socket == null || !client.socket.isConnected())
              client.createSocket();
  
          // Get Input/Output stream for socket
          dataOutputStream = new DataOutputStream(client.socket.getOutputStream());
          dataInputStream = new DataInputStream(client.socket.getInputStream());
  
         // Here RS is simple JAVA class that stores information about payload and response length
          Log.d("Send", "Sending payload for pair " + RS.getc_s_pair() + " " + RS.getResponse_len());
  
          dataOutputStream.write(UtilsManager.serialize(RS.getPayload()));
  
          // Notify waiting Queue thread to start processing next packet
          if (RS.getResponse_len() &gt; 0) {
              Log.d("Response", "Waiting for response for pair " + RS.getc_s_pair() + " of " + RS.getResponse_len() + " bytes");
  
              int totalRead = 0;
  
              byte[] buffer = new byte[RS.getResponse_len()];
              while (totalRead &lt; RS.getResponse_len()) {
                  int bytesRead = dataInputStream.read(buffer, totalRead, RS.getResponse_len() - totalRead);
  
                  if (bytesRead &lt; 0) {
                      throw new IOException("Data stream ended prematurely");
                  }
                  totalRead += bytesRead;
              }
      }
  </code></pre>
  
  <p><strong>Server (Python)</strong></p>
  
  <pre><code>        # Here request_len is the expected request size
          while buffer_len &lt; response_set.request_len:
              new_data = connection.recv( min(self.buff_size, response_set.request_len-buffer_len) )
              if not new_data:
                  return False
              buffer_len += len(new_data)
  
          # Send required response
          for response in response_set.response_list:
              try:
                  connection.sendall(str(response.payload))
              except:
                  return False
  </code></pre>
  
  <p>Sometimes, I am getting error while I'm sending payload from client on line</p>
  
  <pre><code>       dataOutputStream.write(UtilsManager.serialize(RS.getPayload()));
  </code></pre>
  
  <p>Error I'm getting is:</p>
  
  <pre><code>java.net.SocketException: sendto failed: EPIPE (Broken pipe)
      at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
      at libcore.io.IoBridge.sendto(IoBridge.java:475)
      at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
      at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
      at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
      at java.io.DataOutputStream.write(DataOutputStream.java:98)
      at java.io.OutputStream.write(OutputStream.java:82)
      at com.rgolani.replay.tcp.TCPClientThread.run(TCPClientThread.java:56)
      at java.lang.Thread.run(Thread.java:856)
  Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
      at libcore.io.Posix.sendtoBytes(Native Method)
      at libcore.io.Posix.sendto(Posix.java:151)
      at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
      at libcore.io.IoBridge.sendto(IoBridge.java:473)
      ... 7 more
  </code></pre>
  
  <p>I know this is little bit confusing. Python code has not been written by me and there exists a python client which works without any errors. I'm getting this error only in my JAVA code. </p>
  
  <p><strong>Working Python Client</strong></p>
  
  <pre><code>    if self.sock is None:
          self._connect_socket()
  
      self.sock.sendall(tcp.payload)
  
      buffer_len = 0
      while tcp.response_len &gt; buffer_len:
          data = self.sock.recv( min(self.buff_size, tcp.response_len-buffer_len) )
          buffer_len += len(data)
  </code></pre>
  
  <p>Please let me know if you need more information.</p>
  
  <p>Thank you.</p>