当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to handle Socket Exception ideally?

问题描述:

I'm using socket for chat application and I get SocketException unexpectedly. How this exception should ideally be handled? I'm currently simply reconnecting socket. How to make sure my socket is live on application level? I'm aware of method setKeepAlive

网友答案:

A SocketException indicates an error on the network level, usually a TCP/IP error.

Handle this one of two ways: let your application decide if this is a constant issue (firewall, bad connection, etc...) and notify the user, or if this is temporary and can be restored, in which case you should try to reconnect to the socket.

setKeepAlive() has nothing to do with restarting the connection. It only keeps sending keep-alive packets so the connection will not be terminated.

分享给朋友:
您可能感兴趣的文章:
随机阅读: