现在的问题是:对于第二种情况,如果客户端想在发送数据给服务端之前就判断一下连接是否被断开,有什么解决的方法呢?
ps:我的程序是用C#实现的,.Net的socket实现的是Berkeley
Socket。
// .Connect throws an exception if unsuccessful
client.Connect(anEndPoint);
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
try
{
byte [] tmp = new byte[1];
client.Blocking = false;
client.Send(tmp, 0, 0);
Console.WriteLine("Connected!");
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
Console.WriteLine("Still Connected, but the Send would block");
else
{
Console.WriteLine("Disconnected: error code {0}!",
e.NativeErrorCode);
}
}
finally
{
client.Blocking = blockingState;
}
Console.WriteLine("Connected: {0}", client.Connected);
"sunway 写道:
"
> 这类基础问题到CSDN里找找老帖子看看把