System.Net.Sockets.SocketException (0x80004005):一个已建立的连接被您的主

本文关键字:建立 一个 连接 Sockets Net SocketException 0x80004005 System | 更新日期: 2023-09-27 18:08:51

我制作了一个windows服务远程服务器程序,它能够与侦听广播请求的windows CE设备(作为远程客户端)通信,将请求保存到文件中,检查它并将所请求的文件内容发送回跟踪。我已经在(c#)异步传输模式下实现了它。它工作正常,但就在2天前我遇到了这个套接字异常:

System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)

这是我的代码它指向异常

     public void AcceptCallback(IAsyncResult result)
     { 
          ConnectionInfo connection = new ConnectionInfo();
          try
          {
              long connection_number;
              //finish accept
              Socket s;
              s = (Socket)result.AsyncState;
              connection.socket = s.EndAccept(result);
              connection.socket.Blocking = false;
              connection.Buffer = new byte[10000]; //1kb buffer minimum

     orionIP = ((IPEndPoint)connection.socket.RemoteEndPoint).Address.ToString();
               lock (connections) connections.Add(connection);
             IPHostEntry hostName = Dns.GetHostEntry(orionIP);
               machineName = hostName.HostName;
               lock (connections) connections.Add(connection);
                //count client connected
              connection_number = Interlocked.Increment(ref connection_count);
              //start receive -> error here 
   connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length,
                  SocketFlags.None, new AsyncCallback(ReceiveCallBack), connection);
            //start new accept 
             serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
          }//end try 
          catch (SocketException ex)
          { //later log files for error events  - > ex.SocketErrorCode
              string path = "eventlogs.txt";
              //create error log files to write error here 
              File.WriteAllText(path, ex.ToString());
              CloseConnection(connection);
          }
          catch (Exception exp)
          {
              string path = "eventlogs.txt";
              //create error log files to write error here 
              File.WriteAllText(path, exp.ToString());
              CloseConnection(connection);
              // log error file later ("Socket Exception:" + exp.ToString());
          }
     }

发生异常时,我没有对它进行重大修改。我遵循了所有的指示,以及其他人遇到同样的问题,如关闭防火墙,maxusePort,检查网络连接和设备,如果正确配置,缓冲区大小。我也检查竞争条件和WSAECONNABORTED问题,我可能处理。

有谁能给我一些额外的解决方案吗?任何额外的知识都会有很大的帮助。

接收回调

     private void ReceiveCallBack(IAsyncResult result)
     {
         //06.10 added thread for writing in the file 
         //added part review () 
         //getemp file 
         string pathwrite = CreateTmpFile();//"streamdata.txt";
    ConnectionInfo connection = (ConnectionInfo)result.AsyncState;
         try
            { 
             int bytesRead = connection.socket.EndReceive(result);
             if (bytesRead > 0)
             {
                 lock (serverlock)
                 {
                     connection.sb.Append(Encoding.UTF8.GetString(connection.Buffer, 0, bytesRead));
                 }
                 lock (connections)
                 {
                     foreach (ConnectionInfo conn in connections)
                     {
                         if (connection != conn)
                         {
             conn.socket.Send(connection.Buffer, bytesRead, SocketFlags.None);
                         }
                     }
                 }
   connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None,new AsyncCallback(ReceiveCallBack), connection);        
             }
             else
             {
                 string strContent = connection.sb.ToString();
                 File.WriteAllText(pathwrite, strContent);
                AccessData();
                 CloseConnection(connection);
             }
         }//end try 
         catch(SocketException ex){
             string path = "eventlogs2.txt";
             //create error log files to write error here 
             File.WriteAllText(path, ex.ToString());
        // CloseConnection(connection);
         }
         catch(Exception ex)
         {
             string path = "eventlogs2.txt";
             //create error log files to write error here 
             File.WriteAllText(path, ex.ToString());
          //   CloseConnection(connection);
         }

     }

System.Net.Sockets.SocketException (0x80004005):一个已建立的连接被您的主

如果当前接受的连接存在网络错误,则不接受新的连接。将BeginAccept移动到单独的try/catch中。

public void AcceptCallback(IAsyncResult result)
{ 
    ConnectionInfo connection = new ConnectionInfo();
    try
    {
        long connection_number;
        Socket s;
        s = (Socket)result.AsyncState;
        connection.socket = s.EndAccept(result);
        connection.socket.Blocking = false;
        connection.Buffer = new byte[10000]; //1kb buffer minimum

        orionIP = ((IPEndPoint)connection.socket.RemoteEndPoint).Address.ToString();
        lock (connections) connections.Add(connection);
        IPHostEntry hostName = Dns.GetHostEntry(orionIP);
        machineName = hostName.HostName;
        lock (connections) connections.Add(connection);
        connection_number = Interlocked.Increment(ref connection_count);
        connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length,
        SocketFlags.None, new AsyncCallback(ReceiveCallBack), connection);
    }
    catch (SocketException ex)
    {
        string path = "eventlogs.txt";
        File.WriteAllText(path, ex.ToString());
        CloseConnection(connection);
    }
    catch (Exception exp)
    {
        string path = "eventlogs.txt";
        File.WriteAllText(path, exp.ToString());
        CloseConnection(connection);
    }
    try
    {
        serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
    }
    catch (Exception exception)
    {
        //failed to accept. shut down service.
    }
}