2

The answer, here, by Stephen C very well describes the issue. He says Broken pipe exception is caused by something causing the connection to close, and its not the application. I want to know what all this "something" could be in general, which is causing the connection to close? And what are the possible ways to handle them?

My usage environment: I am running my application on set of machines on Azure, and all of them are talking to one of the machine. I am getting this error almost always.

Could TCP timeout be one of the reasons? If yes then how to make Socket Channels(in affect Socket running behind them) never close dues to TCP timeout?

Community
  • 1
  • 1
PHcoDer
  • 1,166
  • 10
  • 23

2 Answers2

2

Broken pipe exception comes whenever client moves away from the socket on which it is listening. This might be due to socket timeout reached on client side as server was responding slow. Say, in case of a browser if any http request is taking long to respond and user closes the browser a broken pipe exception will be visible in application logs.

Now to resolve this either you increase socket timeout or fix you server response.

Vineet Kasat
  • 994
  • 7
  • 14
  • Thanks @VineetKasat How can I increase the socket timeout? I tried looking in [java doc][1] but couldn't find something appropriate. [1] https://docs.oracle.com/javase/7/docs/api/java/nio/channels/SocketChannel.html – PHcoDer Jun 03 '16 at 06:35
  • how are you communicating across machines. In java it is just a simple property to be modified. – Vineet Kasat Jun 03 '16 at 06:40
  • Thanks @Vineet. I am using SocketChannels and read write operation on these channels. What property is that? Are you talking about setSoTimeout()(which is used for normal sockets?) – PHcoDer Jun 03 '16 at 07:11
  • Yes, I was talking about the same property. – Vineet Kasat Jun 03 '16 at 11:22
  • Thanks @Vineet. But the java doc for SocketChannel doesn't shows about any such property. – PHcoDer Jun 03 '16 at 14:47
2

You can get the socket associated with SocketChannel and then set its keepAlive property. Something like this.

SocketChannel sockChannel;
/*
connect here
*/    
sockChannel.socket().setKeepAlive(true);
Chor Sipahi
  • 546
  • 3
  • 10