dtu socket开发, 异常信息: SocketException 堆栈: 在 Socket.AcceptC

时间:2022-02-06 23:56:20
问题:
客户端是多个gprs dtu做透明传输,
服务器端service开始正常运行几分钟,之后就自动停止了,搞了好久,不知道什么引起的,请指教!
从日志看不出什么线索.

异常:
应用程序: REMSServerService.exe Framework 
版本: v4.0.30319 
说明: 由于未经处理的异常,进程终止。
异常信息: System.Net.Sockets.SocketException 堆栈: 在 System.Net.Sockets.Socket.AcceptCallback(System.Object) 在 System.Net.Sockets.Socket.RegisteredWaitCallback(System.Object, Boolean) 在 System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(System.Object, Boolean) 

代码:

Part I:

   protected override void OnStart(string[] args)
    {
            IPEndPoint endpoint = new IPEndPoint(RemsSocket.myip, RemsSocket.webport);
            Socket listensocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listensocket.Bind(endpoint);
            listensocket.Listen(200);
            Thread th = new Thread(new ParameterizedThreadStart(RemsSocket.listen_send));
            th.IsBackground = true;
            th.Start(listensocket);
    }

Part II:

    public static void listen_send(object source)
    {
        while (true)
        {
            done.Reset();
            Socket listensocket = (Socket)source;
            listensocket.BeginAccept(new AsyncCallback(acceptcallback_send), listensocket);
            done.WaitOne();
        }
    }
 
    public static void acceptcallback_send(IAsyncResult ar)
    {
        done.Set();
        Socket socket = (Socket)ar.AsyncState;
        Socket listensocket2 = socket.EndAccept(ar);
        byte[] buffer_validate = new byte[buffersize];
        string hostcode = "";
        byte[] buffer_send = Hex.HexStringToByteArray(validate_code);
        listensocket2.Send(buffer_send, 0, buffer_send.Length, SocketFlags.None);
        listensocket2.Receive(buffer_validate);
        int readsize = buffer_validate.Length; //listensocket2.EndReceive(ar);
        if (readsize > 0)
        {
            bool success = FirstValidate(buffer_validate, ref hostcode);
            bool validate = BLL.ExtHostBLL.CanCommunicate(hostcode); 
            if (success && validate)
            {
                LoopSendReceive(hostcode, listensocket2);
            }
        }
    }

Part III:

    public static void LoopSendReceive(string hostcode, Socket listensocket2)
    {
        listensocket2.Blocking = true;
        byte[] buffer_receive = new byte[buffersize];
        bool validate = BLL.ExtHostBLL.CanCommunicate(hostcode); 
        while (validate)
        {
            Thread.Sleep(500);
            int readsize;
            bool success;
            IPEndPoint clientipe = (IPEndPoint)listensocket2.RemoteEndPoint;
            byte commandByte = Hex.HexStringToByteArray(command_8);
            listensocket2.Send(commandByte, 0, commandByte.Length, SocketFlags.None);
            listensocket2.ReceiveTimeout = 5000;
            int countSocketException = 0;
//异常在这里
readsize = listensocket2.Receive(buffer_receive);
            if (readsize != 0)
                {
                    clientipe = (IPEndPoint)listensocket2.RemoteEndPoint;
                    ClientDto client = hostList.FirstOrDefault(c => c.Ip == clientipe.Address.ToString());
                    ParseChannel.AlertStatus[] data;
                    ParseChannel channel = new ParseChannel();
                    success = channel.Parse(buffer_receive, client, out data);
                    if (!success)
                    {
                        continue;
                    }
                    SaveData(data, client);
                }
        }
    }

1 个解决方案

#1


dtu socket开发, 异常信息: SocketException 堆栈: 在 Socket.AcceptCdtu socket开发, 异常信息: SocketException 堆栈: 在 Socket.AcceptC

#1


dtu socket开发, 异常信息: SocketException 堆栈: 在 Socket.AcceptCdtu socket开发, 异常信息: SocketException 堆栈: 在 Socket.AcceptC