日期:2014-05-18  浏览次数:21227 次

C# Socket 捕获不到异常
C# code

                IPEndPoint myServer = new IPEndPoint(IPAddress.Parse(textBox_ServerIp.Text), 5588);
                _tcpListener = new TcpListener(myServer);
                _tcpListener.Start();
                while (true)
                {
                    try
                    {
                        TcpClient ss = _tcpListener.AcceptTcpClient();
                        Thread t = new Thread(RecieveData);
                        t.Start(ss);

                    }
                    catch (Exception ex) { Common.CommonRoutine.HandlingExceptions(ex); }
                }

        public void RecieveData(object obj)
        {
            TcpClient tcpClient = obj as TcpClient;
            StateObject stateObject = new StateObject() { WorkSocket = tcpClient };
            while (true)
            {
                try
                {
                    if (tcpClient != null && tcpClient.Client != null )
                        tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None,
                                                      RecieveComplete, stateObject);
                    stateObject.ResetEvent.WaitOne();
                   // Thread.Sleep(1000);
                    //MessageBox.Show("s");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
   
            }
        }


        public void RecieveComplete(IAsyncResult result)
        {
            StateObject state = (StateObject)result.AsyncState;
            TcpClient tcpClient = state.WorkSocket;
            if (tcpClient == null || tcpClient.Client == null || !tcpClient.Client.Connected)
            {
                return;
            }
            int bytesRed = tcpClient.Client.EndReceive(result);
            if (bytesRed > 0)
            {
                byte[] receData = new byte[bytesRed];
                Array.Copy(state.Buffer, 0, receData, 0, bytesRed);
                byte[] realData = OperateCmdData.RestoreReceData(receData);
                if (OperateCmdData.JudgeisCompleteData(realData))
                {
                    byte[] sendData = OperateCmdData.AnalysisMessageId(receData);
                    if (sendData.Length > 19)
                        tcpClient.Client.Send(sendData);
                }
            }
            state.ResetEvent.Set();
        }




RT 客户端异常关闭socket服务端程序会直接退出,捕获不到任何异常。如果在BeginReceive那里睡1S钟就可以捕获到“远程主机强迫关闭了一个现有的连接。”异常。

C# code
                       tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None,
                                                      RecieveComplete, stateObject);
                    stateObject.ResetEvent.WaitOne();
                   Thread.Sleep(1000);


如果不睡1S的话程序会自动退出。看VS的输出消息有
在 System.Net.Sockets.SocketException 中第一次偶然出现的“System.dll”类型的异常

求解。3Q。

------解决方案--------------------
C# code

while (true)
            {
                try
                {
                    if (tcpClient != null && tcpClient.Client != null )
                        tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None,
                                                      RecieveComplete, stateObject);
                    stateObject.ResetEvent.WaitOne();
                   // Thread.Sleep(1000);
                    //MessageBox.Show("s");
                }
                catch (Exception ex)
                {