JavaSocket异常和若干细节
SocketTimeoutException
Socket
连接超时设置connect(SocketAddress endpoint, int timeout) throws IOException
SocketTimeoutException if timeout expires before connecting
读超时设置
public synchronized void setSoTimeout(int timeout) throws SocketException
public void setTcpNoDelay(boolean on) throws SocketException
对于实时性要求高的应用 如telnet 网游 该参数设置为true
vc下面socket编程 使用阻塞方式会自动使用Nagle算法
如当pc不断发送32Bytes数据 会将这些包合并起来一起发送
实际使用的时候 header body 分开发送 另一端处理可能为出问题
write(header) write(body)
Nagle虽然解决了小封包问题 但也导致了较高的不可预测的延迟 同时降低了吞吐量
java.net.BindException:Address already in use: JVM_Bind 端口被占用 netstat -an 查看
java.net.ConnectException: Connection refused: connect
客户端无法建立连接 譬如服务器没启动 或 ip 端口不对
java.net.SocketException: Socket is closed 调用了Socket close方法 再进行读写操作
java.net.SocketException: Connection reset 一端关闭 另一端读操作
java.net.SocketException: Connect reset by peer:Socket write error 一端关闭 另一端写操作
java.net.SocketException: Broken pipe 上一个异常后继续写
长连接 VS 短连接
长连接保持 心跳检测 连接 读超时设置
Socket有一个keep alive来维持 不过基本上没用 需要很长时间才能检测到异常