12

I am trying to make connection with server using socket. The connection pipe is broken as shown below exceptions.

01-31 14:47:16.536: W/System.err(27255): java.net.SocketException: sendto failed: EPIPE (Broken pipe)
01-31 14:47:16.550: W/System.err(27255):    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:496)
01-31 14:47:16.550: W/System.err(27255):    at libcore.io.IoBridge.sendto(IoBridge.java:465)
01-31 14:47:16.550: W/System.err(27255):    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
01-31 14:47:16.550: W/System.err(27255):    at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
01-31 14:47:16.664: W/NetworkManagementSocketTagger(24437): setKernelCountSet(10021, 1) failed with errno -2
01-31 14:47:16.684: W/System.err(27255):    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
01-31 14:47:16.693: W/System.err(27255):    at java.io.DataOutputStream.write(DataOutputStream.java:98)
01-31 14:47:16.693: W/System.err(27255):    at java.io.OutputStream.write(OutputStream.java:82)
01-31 14:47:16.693: W/System.err(27255):    at com.x.x.y.sendRec(y.java:460)
01-31 14:47:16.693: W/System.err(27255):    at com.x.x.y.access$0(y.java:384)
01-31 14:47:16.693: W/System.err(27255):    at com.x.x.y$2.run(y.java:363)
01-31 14:47:16.693: W/System.err(27255):    at java.lang.Thread.run(Thread.java:856)
01-31 14:47:16.693: W/System.err(27255): Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
01-31 14:47:16.693: W/System.err(27255):    at libcore.io.Posix.sendtoBytes(Native Method)
01-31 14:47:16.693: W/System.err(27255):    at libcore.io.Posix.sendto(Posix.java:146)
01-31 14:47:16.693: W/System.err(27255):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
01-31 14:47:16.693: W/System.err(27255):    at libcore.io.IoBridge.sendto(IoBridge.java:463)

Here is the code and on this line outStreamRec.write(bData); throwing exception.

    try {
        port = 86;
        byterecv = new byte[1040];
        clientRec = new Socket();
        clientRec.connect(new InetSocketAddress("192.168.1.36", port));
        System.out.println("Just connected to " + clientRec.getRemoteSocketAddress());
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    while (true) {
        try {
            System.out.println("Connecting to " + ServerUrl.url + " on port " + port);              
            OutputStream outToServerRec = clientRec.getOutputStream();
            DataOutputStream outStreamRec = new DataOutputStream(outToServerRec);

            outStreamRec.write(bData);

            InputStream inFromServerPlay = clientRec.getInputStream();
            DataInputStream inStreamPlay = new DataInputStream(inFromServerPlay);
            while ((lstream = inStreamPlay.read(byterecv)) != -1) {
                System.out.println("startrec bytearray -- " + byterecv.length);
                bos1.write(byterecv, 0, lstream);
            }                
            if (stopcall == true) {
                clientRec.close();
                break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Note: if I close the socket connection immediate works fine. But I want to keep the connection alive still I close the socket connection manually. Onclick of stop button I am closing the connection.

I searched the web, but didn't find a way to fix this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

4 Answers4

11

The peer closed the connection while you were writing to it. That usually means you sent it something it didn't understand. Is it perhaps an HTTP server? Or some other protocol that you haven't implemented in your client code?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    But what can I do to rectify that? That is the significant question here. – Sreekanth Karumanaghat Aug 29 '13 at 06:25
  • 2
    @SreekanthKarumanaghat *Don't write to the connection after the peer has closed it!* That indicates an application protocol error on your part. Either the peer has closed when it shouldn't have, or you are sending when you shouldn't. – user207421 Sep 01 '13 at 10:43
  • By saying "The **peer** closed the connection..." you mean "The **server** closed the connection..."? – Eido95 Jun 22 '17 at 09:22
  • 1
    @Eido95 No, I dIdn't. I meant what I said. I mean the peer. Once a TCP connection is established, it is pointless to keep thinking about server and client. They are both peers of each other. It would only triple the size of the answer to talk about the server if this end is the client, or the client if this end is the server. It doesn't matter in the least. What matters is that it is the peer who closed, not the local end. – user207421 Jun 22 '17 at 12:02
3

My two cents: we had the same issue (BROKEN EPIPE), and looking through Fiddler (or Charls, or WireShark, or other proxy debugger / listener / etc) we've noticed that no request gets sent out at all.

The reason was that we added "Content-Length" header with the wrong value.

halfer
  • 19,824
  • 17
  • 99
  • 186
Felix
  • 1,034
  • 1
  • 9
  • 29
  • You probably shouldn't have added it at all. Probably the library does it for you. `HttpURLConnection` certainly does. – user207421 Apr 01 '16 at 17:02
  • 1
    I verified with the developer who wrote it, and he said that if he remembers correctly, the problem was that the header isn't added when using `setChunkedStreamingMode(x)` (because it's prohibited, see [Wikipedia](https://en.wikipedia.org/wiki/Chunked_transfer_encoding)), but the the MS-WCF binding the server used requires it, even when Chunked Streaming is on. – Felix Apr 03 '16 at 10:13
  • 1
    Then that's a bug in MS-WCF. – user207421 Feb 07 '17 at 09:08
0

I met this kind issue on a Samsung Tablet(GT-P5113, Android 4.2.2) device, the application works well on other devices(Nexus 4/5/7).

The code in the Libcore/io/IoBridge.java looks like :

public static int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws IOException {
    boolean isDatagram = (inetAddress != null);
    if (!isDatagram && byteCount <= 0) {
        return 0;
    }
    int result;
    try {
        result = Libcore.os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port);
    } catch (ErrnoException errnoException) {
        result = maybeThrowAfterSendto(isDatagram, errnoException);
    }
    return result;
}

while the Libcore.os.sendto() is a native call.

Maybe try one more time is a good candidate for workaround.

Zephyr
  • 6,123
  • 34
  • 33
0

Happened to me too, and the silly reason was that the Internet on the device was not working properly.

Check your internet connection, this could be the reason.

Danger
  • 433
  • 5
  • 17
  • 1
    technically he is right though. Whoever downvoted this will make some silly mistake like that at some point for sure. – CiriousJoker Apr 19 '20 at 20:39