当我尝试重新建立与Socket的连接时,出现以下错误:
错误:通常只允许使用每个套接字地址(协议/网络地址/端口) SocketErrorCode :System.Net.Sockets.SocketError.AddressAlreadyInUse
每次我想重新连接时,我首先调用Disconnect()方法。但是,即使我的套接字断开连接,当我尝试使用Connect()方法重新连接时出现错误。
public SocketError Connect()
{
//Open socket connection
SocketAsyncEventArgs connectArgs = new SocketAsyncEventArgs();
connectArgs.UserToken = _socket;
connectArgs.RemoteEndPoint = _hostEndPoint;
connectArgs.Completed += OnConnect;
_socket.ConnectAsync(connectArgs); //ERROR : Only one usage of each socket address...
_autoConnectEvent.WaitOne();
if (connectArgs.SocketError != SocketError.Success)
{
throw new SocketException((Int32)connectArgs.SocketError);
}
return connectArgs.SocketError;
}
public void Disconnect()
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Disconnect(true);
}
有谁知道如何避免此错误?
答案 0 :(得分:1)
此处的说明:Only one usage of each socket address (protocol/network address/port) is normally permitted
谢谢@competent_tech