如何查找客户端套接字是否已连接到服务器 [英] How to find whether client socket is connected to server or not

查看:113
本文介绍了如何查找客户端套接字是否已连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写代码来查找客户端套接字是否已连接。

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'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


这篇关于如何查找客户端套接字是否已连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆