c#判断服务器是否在线

TCP设计无法获取实时连线状况,只有在Send时会主动产生Exception返回状态,

因此可以通过每次操作之前写入一个字符来判断服务器是否在线

try
{
//写入
Clients[i].Client.Send(buffer);
//读取
int bytesRead = Clients[i].Client.Receive(bytes);
//将读取的数据写入应答队列
if (bytesRead > 0)
{
element_response.data = Encoding.ASCII.GetString(bytes, 0, bytesRead);
}
}
catch (SocketException)
{
Client_statues[i] = false;
}

posted @ 2019-11-29 16:57  天才米布袋  阅读(1245)  评论(0编辑  收藏  举报