Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Net.Sockets.Sockets.AcceptCallback中的System.Net.Sockets.SocketException堆栈_C#_Sockets - Fatal编程技术网

C# System.Net.Sockets.Sockets.AcceptCallback中的System.Net.Sockets.SocketException堆栈

C# System.Net.Sockets.Sockets.AcceptCallback中的System.Net.Sockets.SocketException堆栈,c#,sockets,C#,Sockets,请看下面的代码,它在我的windows服务中不是很稳定,启动一段时间后会抛出错误: 例外情况如下,请有人帮忙吗?应用程序: REMSServerService.exe框架版本:v4.0.30319说明: 未知异常。信息:中的System.Net.Sockets.SocketException堆栈 中的System.Net.Sockets.Socket.AcceptCallback(System.Object) System.Net.Sockets.Socket.RegisteredWaitCal

请看下面的代码,它在我的windows服务中不是很稳定,启动一段时间后会抛出错误:

例外情况如下,请有人帮忙吗?应用程序: REMSServerService.exe框架版本:v4.0.30319说明: 未知异常。信息:中的System.Net.Sockets.SocketException堆栈 中的System.Net.Sockets.Socket.AcceptCallback(System.Object) System.Net.Sockets.Socket.RegisteredWaitCallback(System.Object, 布尔值)在 System.Threading.\u ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(系统‌​m、 反对,, 布尔值)

在以下位置捕捉到错误:readsize=listensocket2.Receive(缓冲区\接收)

该项目是一个数据通信中心,客户端是GPRS DTU套接字客户端

请帮忙

第一部分:

    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);
    }
第二部分:

    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);
            }
        }
    }
第三部分:

    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);
                }
        }
    }

你需要知道SocketException中有哪个错误代码才能真正知道发生了什么最后一个错误代码是成功的,另一个是超时,经常抛出。然后很明显,接收功能超时,因为另一方没有发送数据或数据包丢失(如果是GPRS,那么我的投票支持丢失的数据包),如果是这种情况,那么您需要一种机制,当您捕获到一个timeoutPost时,再次向另一端查询相同的数据包,该timeoutPost包含完整的异常ToString。你犯了一个常见的错误,认为Receive会收到一条完整的“消息”。TCP是一种流协议。