Java.net.SocketException: sendto failed: EPIPE (Broken pipe) on Android

I am trying to read data from a server socket from a client in Android.

Here are snippets of the code I'm using:

Client (on Android in JAVA)

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() > 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 < RS.getResponse_len()) { int bytesRead = dataInputStream.read(buffer, totalRead, RS.getResponse_len() - totalRead); if (bytesRead < 0) { throw new IOException("Data stream ended prematurely"); } totalRead += bytesRead; } } 

Server (Python)

  # Here request_len is the expected request size while buffer_len < 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 

Sometimes I get an error while I send a payload from a client on the line

  dataOutputStream.write(UtilsManager.serialize(RS.getPayload())); 

The error I am getting is:

 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 

I know this is a bit confusing. Python code was not written by me and there is a python client that works without errors. I get this error only in my JAVA code.

Python working client

  if self.sock is None: self._connect_socket() self.sock.sendall(tcp.payload) buffer_len = 0 while tcp.response_len > buffer_len: data = self.sock.recv( min(self.buff_size, tcp.response_len-buffer_len) ) buffer_len += len(data) 

Please let me know if you need more information.

Thanks.

+6
source share
4 answers

If you are using setChunkedStreamingMode then delete it and then see. I think I'm so late, but it solves my problem. You can use fixed stream streaming mode.

+1
source

This error occurs when a client tries to record a connection when the backend has already closed it. By default, Android set the Keep Alive property to true and reuse of old socket connections, since establishing a connection is a resource-intensive operation in a mobile environment (you can test it with a tool like fiddler ). Try setting the Connection header to close or calling System.setProperty("http.keepAlive", "false"); to disable Keep Alive and thus force the system to create a new connection.

+1
source

I may be missing a point with your problem, but I think it would be better to use this Java fragment:

 import java.io.ObjectOutputStream; import java.io.ObjectInputStream; ObjectOutputStream dataOutputStream = null; ObjectInputStream dataInputStream = null; try { if (client.socket == null || !client.socket.isConnected()) client.createSocket(); // Get Input/Output stream for socket dataOutputStream = new ObjectOutputStream (client.socket.getOutputStream()); dataInputStream = new ObjectInputStream (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()); // You might consider using java.io.Serializable interface with the argument object dataOutputStream.writeObject(UtilsManager.serialize(RS.getPayload())); // Notify waiting Queue thread to start processing next packet if (RS.getResponse_len() > 0) { Log.d("Response", "Waiting for response for pair " + RS.getc_s_pair() + " of " + RS.getResponse_len() + " bytes"); // Read a java.io.Serializable object from the stream. So you have to write a java.io.Serializable object into the stream. Object o = dataInputStream.readObject(); } } 

Hope this helps ...

0
source

In my case: client.createSocket (); don't connect to any server - just create a socket. So later I need client.connect (_sock_sddr_here _);

Also, isConnected () is a dumb test because it is forever right.

0
source

All Articles