且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查找客户端套接字是否已连接到服务器

更新时间:2022-02-10 22:18:30

Android的套接字.isConnected()将返回 true 而不是 false 如果套接字未正确断开连接(即未调用 Socket.close )。



我认为唯一保证知道 Socket 是否还活着的方法是尝试写入并处理 IOException 如果没有连接,它会抛出。



希望这会有所帮助,

Fredrik
Android's Socket.isConnected() will return true instead of false if the Socket was disconnected incorrectly (i.e. not calling Socket.close).

I think the only guaranteed way of knowing if a Socket is still alive is to try to write to it and handle the IOException that it will throw if it is not connected.

Hope this helps,
Fredrik