如何查找客户端套接字是否已连接到服务器 [英] How to find whether client socket is connected to server or not
本文介绍了如何查找客户端套接字是否已连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写代码来查找客户端套接字是否已连接。
if(socket.isConnected())
{
//如果断开连接也会让它恢复正常;
}
其他
{
}
但是客户端断开连接也是真的;请告诉我如何查找客户端套接字是否连接到服务器。
i written code to find client socket is connected or not.
if(socket.isConnected())
{
//if disconnected also it returing true;
}
else
{
}
But client disconnected also it returing true;Please tell me How to find whether client socket is connected to server or not.
推荐答案
Android的套接字.isConnected()
将返回true
而不是false
如果套接字
未正确断开连接(即未调用Socket.close
)。
我认为唯一保证知道Socket
是否还活着的方法是尝试写入并处理IOException
如果没有连接,它会抛出。
希望这会有所帮助,
Fredrik
Android'sSocket.isConnected()
will returntrue
instead offalse
if theSocket
was disconnected incorrectly (i.e. not callingSocket.close
).
I think the only guaranteed way of knowing if aSocket
is still alive is to try to write to it and handle theIOException
that it will throw if it is not connected.
Hope this helps,
Fredrik
这篇关于如何查找客户端套接字是否已连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文