请教关于SOCKET断开再重新使用此TcpClient去连接服务器,会报出SOCKET正在连接的错误
代码如下:
TcpClient FTcpClient=new TcpClient();
FTcpClient.ReceiveTimeout = 10;
FTcpClient.SendTimeout = 10;
Host = "127.0.0.1 ";
Port = 9000;
pClient.ConnectSvr()
FTcpclient.DisconnectSvr();
FTcpClient.ConnectSvr(); --> 这里报错,10056错误, 错误信息是Socket is already connected
public bool ConnectSvr()
{
try
{
if (!FTcpClient.Connected)
FTcpClient.Connect(FHost, FPort);
return true;
}
catch (SocketException e)
{
return false;
}
}
public void DisconnectSvr()
{
if (FTcpClient.Connected)
{
FTcpClient.Client.Shutdown(SocketShutdown.Both);
FTcpClient.Client.Close();
}
}
始终没弄明白这是为什么,大家能指点一下嘛,谢谢了
------解决方案--------------------pClient.ConnectSvr()还占用着socket的啊!
先pClient.DisconnectSvr()
------解决方案--------------------