Home > Net >  The Socket communication "System.Net.Sockets.SocketException x80004005 (0)" problem
The Socket communication "System.Net.Sockets.SocketException x80004005 (0)" problem

Time:11-09

Recently made a simple small program of SOCKET communication, used to achieve a small function, namely receive data, database,

But in actual operation, appeared a little problem, when the program is running after a period of time (about 5 hours), will be an error:

X80004005 System.Net.Sockets.SocketException (0) : caused by a lack of buffer space or queue is full, can't perform on the socket operation,
In System.Net.Sockets.Socket.BeginReceive (Byte [] buffer, Int32 offset, Int32 size, SocketFlags SocketFlags, AsyncCallback callback, the Object state)

Already checked three or four days, has no clue!

In accordance with the said online registry Settings are set, still can't solve the problem,

Looking at hand know a thing or two!

Released a problem of code below

CodePudding user response:


IP public SocketManager (string, int port)
{
_socket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
IPAddress _ip=IPAddress. Parse (IP);
_endPoint=new IPEndPoint (_ip, port);
_listSocketInfo=new Dictionary (a);
_DeviceInfo=new Dictionary (a);
}

Public void the Start ()
{
_socket. Bind (_endPoint);//bind port
_socket. Listen (50);//open listening
Thread acceptServer=new Thread (AcceptWork);//open a new thread handle listening
AcceptServer. IsBackground=true;
_isListening=true;
AcceptServer. Start ();
}

Public void AcceptWork ()
{
While (_isListening)
{
The Socket acceptSocket=_socket. The Accept ();
If (acceptSocket!=null & amp; & This OnConnected!=null)
{
SocketInfo sInfo=new SocketInfo ();
SInfo. Socket=acceptSocket;
if (! _listSocketInfo. Keys. The Contains (acceptSocket. RemoteEndPoint. ToString ()))
{
_listSocketInfo. Add (acceptSocket. RemoteEndPoint. ToString (), sInfo);
}
The else
{//release the last link, establish rebind
_listSocketInfo [acceptSocket RemoteEndPoint. ToString ()], isConnected=false;
_listSocketInfo [acceptSocket RemoteEndPoint. ToString ()]. Socket. The Shutdown (SocketShutdown. Both);
_listSocketInfo [acceptSocket RemoteEndPoint. ToString ()]. The socket. The Close ();
_listSocketInfo [acceptSocket RemoteEndPoint. ToString ()]=sInfo;
}
OnConnected (acceptSocket. RemoteEndPoint. ToString ());
Thread socketConnectedThread=new Thread (newSocketReceive);
SocketConnectedThread. IsBackground=true;
SocketConnectedThread. Start (acceptSocket);
}
Thread.sleep (1000);
}
}

Public void newSocketReceive (object obj)
{
The Socket Socket=obj as Socket;
String the EndPoint=socket. RemoteEndPoint. ToString ();
SocketInfo sInfo=_listSocketInfo [the EndPoint];
SInfo. IsConnected=true;
While (sInfo. IsConnected)
{
Try
{
If (sInfo. Socket==null | |! SInfo. Socket. Connected)
{
LogHelper. WriteDebug (" socket=null IP="+ the EndPoint).
SInfo. IsConnected=false;
If (_listSocketInfo. Either ContainsKey (EndPoint))
{
_listSocketInfo. Remove (EndPoint);

}
SInfo. Socket. Shutdown (SocketShutdown. Both);
SInfo. Socket. The Close ();
return;
}
//here to deliver a request to receive information system, and for its designated ReceiveCallBack as the callback function
SInfo. Socket. BeginReceive (sInfo buffer, 0, sInfo. Buffer. The Length, SocketFlags. None, ReceiveCallBack, sInfo. Socket. RemoteEndPoint);
}
The catch (Exception ex)
{
LogHelper. WriteDebug (" [Exception] Method=newSocketReceive | | Exception="+ ex. The ToString ());
SInfo. IsConnected=false;

If (_listSocketInfo. Either ContainsKey (sInfo. Socket. RemoteEndPoint. ToString ()))
{
_listSocketInfo. Remove (sInfo. Socket. RemoteEndPoint. ToString ());
}

Var index="";
The foreach (var item in _DeviceInfo)
{
If (item) Value=https://bbs.csdn.net/topics/=sInfo) socket. RemoteEndPoint. The ToString ())
{
The index=item. The Key;
_DeviceInfo. Remove (index);
break;
}
}
SInfo. Socket. Shutdown (SocketShutdown. Both);
SInfo. Socket. The Close ();
return;
}
Thread.sleep (1000);
}
}
 

CodePudding user response:

IP
 public SocketManager (string, int port) 
{
_socket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
IPAddress _ip=IPAddress. Parse (IP);
_endPoint=new IPEndPoint (_ip, port);
_listSocketInfo=new Dictionary (a);
_DeviceInfo=new Dictionary (a);
}

Public void the Start ()
{
_socket. Bind (_endPoint);//bind port
_socket. Listen (50);//open listening
Thread acceptServer=new Thread (AcceptWork);//open a new thread handle listening
AcceptServer. IsBackground=true;
_isListening=true;
AcceptServer. Start ();
}

Public void AcceptWork ()
{
While (_isListening)
{
The Socket acceptSocket=_socket. The Accept ();
If (acceptSocket!=null & amp; & This OnConnected!=null)
{
SocketInfo sInfo=new SocketInfo ();
SInfo. Socket=acceptSocket;
if (! _listSocketInfo. Keys. The Contains (acceptSocket. RemoteEndPoint. ToString ()))
{
_listSocketInfo. Add (acceptSocket. RemoteEndPoint. ToString (), sInfo);
}
The else
{//release the last link, establish rebind
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related