C#套接字错误10022 [英] C# socket error 10022
问题描述
我在使用套接字在C#.NET3.5应用程序错误10022。
在我的代码,我绑定套接字到本地IP地址。
当我不吨需要它了,我不去想它(reader.socket.Disconnect(真);)与真,以便能够重新使用它
但是,当我再次拨打了绑定的方法,它与10022错误(无效参数)崩溃。
如果我设置了符合这一方法为注释,它就会崩溃就行了听,他说,连接已经设置(虽然我称为断开!)
你知道吗?
感谢
下面是一个失败的代码部分:
公共无效WaitConnexion(IPEndPoint localEP)
{
如果(localEP.Port!= 9000)
{
MessageBox.Show(乐口DOIT理由9000);
的回报;
}
LocalEndPoint = localEP;
如果(阅读器。 socket.Connected)
{
MessageBox.Show(VOUSêtes似曾相识connecté,争端中去联接,MessageBoxButton.OK,MessageBoxImage.Exclamation);
的回报;
}
//上绑定乐插座AVEC乐终点的地方,上乐等会见恩attente德联接asynchrone
// reader.socket =新的Socket( AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
reader.socket.Bind(localEP);
reader.socket.Listen(1);
reader.socket.BeginAccept(新的AsyncCallback(WaitConnexionCallBack),reader.socket);
}
和这里是diconnect方法,其回调:
公共无效断开()
{
如果
回报(reader.socket.Connected!);
reader.socket.BeginDisconnect(真,新的AsyncCallback(DisconnectCallBack),reader.socket);
}
私人无效DisconnectCallBack(IAsyncResult的结果)
{
reader.socket =(result.AsyncState如插座);
reader.socket.EndDisconnect(结果);
如果(断开!= NULL)
断开(这一点,EventArgs.Empty);
}
我找到了解决办法。
看来,这不是叫垃圾收集足够快,在内存让这哪里实际部署一些插座的价值。
修改的代码在DisconnectedCallBack:
reader.socket =(结果.AsyncState作为插座);
reader.socket.EndDisconnect(结果);
reader.socket.close(0);
GC.Collect的(); //调用垃圾收集清理插座
修改代码在WaitConnexion:
reader.socket =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
重新创建套接字。
I have an error 10022 in an application using sockets in C# .NET3.5.
In my code, I bind the socket to the local IP adress. When I don"t need it anymore, I just Disconnect it ( reader.socket.Disconnect(true); ) with "true" to be able to re-use it.
But when I call the "bind" method again, it crashes with the 10022 error (invalid argument).
If I set the line with this method as a comment, it then crashes on the line "listen", saying that a connection is already set (although I called disconnect !)
Any idea?
Thanks
Here is the part of code which fail :
public void WaitConnexion(IPEndPoint localEP)
{
if (localEP.Port != 9000)
{
MessageBox.Show("Le port doit être 9000");
return;
}
LocalEndPoint = localEP;
if (reader.socket.Connected)
{
MessageBox.Show("Vous êtes déjà connecté", "Conflit de connexion", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
// on bind le socket avec le endpoint local, et on le met en attente de connexion asynchrone
// reader.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
reader.socket.Bind(localEP);
reader.socket.Listen(1);
reader.socket.BeginAccept(new AsyncCallback(WaitConnexionCallBack), reader.socket);
}
and here is the diconnect method with its callback :
public void Disconnect()
{
if (!reader.socket.Connected)
return;
reader.socket.BeginDisconnect(true, new AsyncCallback(DisconnectCallBack), reader.socket);
}
private void DisconnectCallBack(IAsyncResult result)
{
reader.socket = (result.AsyncState as Socket);
reader.socket.EndDisconnect(result);
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
}
I found the solution.
It seems that the garbage collector wasn't called fast enough, letting in memory some socket's value which where actually disposed.
Code Modification in DisconnectedCallBack :
reader.socket = (result.AsyncState as Socket);
reader.socket.EndDisconnect(result);
reader.socket.close(0);
GC.Collect(); // call garbage collector to clean the socket
Code modification in WaitConnexion :
reader.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Re-creating the socket.
这篇关于C#套接字错误10022的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!