簡體   English   中英

System.Net.Sockets.SocketException(0x80004005):建立的連接已被主機中的軟件中止

[英]System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine

我制作了一個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);
         }


     }

如果當前接受的連接出現網絡錯誤,則您不接受新的連接。 將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.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM