TCP服务器ReadAsync值得EndRead

时间:2018-04-03 06:53:05

标签: c# multithreading asynchronous tcpserver

通常我可以在这个或其他网站上找到asnwer,文档。但现在我无法理解...... 我有两个版本的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可以帮助我。

1 个答案:

答案 0 :(得分:1)

对不起这是相同的工作,但ReadAsync在我看来更快,更清晰。我在其他代码中犯了错误,我每次都向GPRS调制解调器发送两次数据包,并且调制解调器将两个答案合二为一。

在初学者我必须使用不同的服务器,当我更改ReadAsync(BeginRead,EndRead)时,我看到同样的错误。并找到了它。