.net里的Socket编程怎么解决另外一端突然断开的问题?
网上给的范例,简单socket服务器,在连接完毕后保持接收对方消息时的代码大部分类似下面的
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);
Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
try
{
}
catch e
{
//如果e的message里面有什么"XXXX断开连接"就说明对方下线了.
}