TCP 服务器 ReadAsync 值然后 EndRead

TCP server ReadAsync worth then EndRead

通常我可以在这个网站或其他网站、文档上找到答案。但现在我无法忍受... 我有两个版本的 TCP 服务器代码,(NetworkSteam 读取)。

使用 ReadAsync:

public async Task StartWorkAsync(TcpClient Modem, CancellationToken ct)
        {
            using (Modem)
            {
                    byte[] buf = new byte[1024];
                    var steam = Modem.GetStream();
                    while (!ct.IsCancellationRequested && !StopAllWorkFoClients.WaitOne(0))
                    {
                        var amountReadTask = steam.ReadAsync(buf, 0, buf.Length, ct);
                        var amountRead = await amountReadTask.ConfigureAwait(false);

                        if (amountReadTask.IsFaulted || amountReadTask.IsCanceled)
                        {
                            Console.WriteLine("Error:IsFaulted||IsCanceled");
                            break;
                        }
                        await WorkOnReceiveModemData(buf.Take(amountRead).ToArray()).ConfigureAwait(false);
                    }
            }
        }

还有 BeginRead、EndRead:

        byte[] buf = new byte[1024];
        public void StartWork(TcpClient Modem)
        {
            var steam = Modem.GetStream();
            var x = steam.BeginRead(buf, 0, buf.Length, new AsyncCallback(ReceiveDataAsync), (object)steam);
        }

        public void ReceiveDataAsync(IAsyncResult ar)
        {
            try
            {
                NetworkStream x = (NetworkStream)ar.AsyncState;//(((Tuple<object,object>)ar.AsyncState).Item1);

                try
                {
                    bytesread += x.EndRead(ar);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                var timeout = 0;
                var bytesbefore = bytesread;
                x.ReadTimeout = 50;
                while (bytesread < _BYTESBUFFERCOUNT)
                {
                    DateTime dt = DateTime.Now;
                    if (x.DataAvailable)
                    {
                        var ii = x.Read(bufzzzz, bytesread, bufzzzz.Length - bytesread);
                        bytesread += ii;
                    }

                    System.Threading.Thread.Sleep(10);

                    if (bytesread - bytesbefore > 0)
                        timeout = 0;
                    else
                    {
                        timeout += (int)(DateTime.Now - dt).TotalMilliseconds;
                    }
                    if (bytesread > 5)
                        if (timeout > _ACCUMULATETIMEOUT)
                            break;

                    if (bytesread <= 5)
                        if (timeout > _ACCUMULATETIMEOUT * 2)
                            break;

                    bytesbefore = bytesread;
                }
                WorkOnReceiveModemData(buf.Take(bytesread).ToArray());

                x.BeginRead(buf, bytesread, buf.Length, new AsyncCallback(ReceiveDataAsync), (object)x);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

也许我弄错了,但在第二个版本中它是这样写的(右): 0x00 0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC

但在第一个版本中是这样写的:

读取:0x00 0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC 0x00 0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC

或:

读取:0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC

读取:0x00 0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC

读取:0x00 0x10 0x3F 0xFF 0x00 0x00 0xFD 0xFC

我的客户是不同的 GPRS 调制解调器。希望smb能帮到我

对不起。它是平等的,但我认为 ReadAsync 更快,更清晰。我在其他代码中有错误,我每次都向 GPRS 调制解调器发送两次数据包,调制解调器将两个答案合二为一。

刚开始我不得不使用不同的服务器,当我在 (BeginRead, EndRead) 上更改 ReadAsync 时,我看到了同样的错误。并找到了。