当前位置: 首页>>代码示例>>C#>>正文


C# NetworkStream.ReadExactBytesAsync方法代码示例

本文整理汇总了C#中NetworkStream.ReadExactBytesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.ReadExactBytesAsync方法的具体用法?C# NetworkStream.ReadExactBytesAsync怎么用?C# NetworkStream.ReadExactBytesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NetworkStream的用法示例。


在下文中一共展示了NetworkStream.ReadExactBytesAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SlippiConnection

        public SlippiConnection(TcpClient client, NetworkStream stream)
        {
            //Populated MAC and IP
            DeviceIp = ((IPEndPoint)client.Client.RemoteEndPoint).Address;

            //Make new game info
            GameInfo = new GameInformation();

            //Handle receiving messages
            Observable.Start(async () =>
            {
                try
                {
                    while(client.Connected)
                    {
                        //First always start by reading the size of the message
                        var lengthBytes = await stream.ReadExactBytesAsync(4);
                        var messageLength = BitConverter.ToInt32(lengthBytes.Reverse().ToArray(), 0);

                        //Make sure message length isn't too long
                        if (messageLength > MAX_MESSAGE_LENGTH || messageLength < 0) throw new Exception("Message length was too long or too short. Length: " + messageLength);

                        //Read messageLength bytes
                        var message = await stream.ReadExactBytesAsync(messageLength);

                        //foreach (byte b in message) System.Diagnostics.Trace.WriteLine(string.Format("[{0:X2}]", b));

                        //Pass off message to GameInfo
                        GameInfo.ProcessMessage(message);
                        //var buf = new byte[32];
                        //var amountRead = stream.Read(buf, 0, 32);
                        //for (int i = 0; i < amountRead; i++) System.Diagnostics.Trace.WriteLine(string.Format("[{0:X2}]", buf[i]));
                    }

                    logger.Info("Client connected flag is no longer true.");
                }
                catch (Exception ex)
                {
                    logger.Info(ex, "Connection threw an exception.");
                }
                finally
                {
                    stream.Close();
                    Dispose();
                }
            }, System.Reactive.Concurrency.TaskPoolScheduler.Default);
        }
开发者ID:JLaferri,项目名称:HardwareEnhancedMelee,代码行数:47,代码来源:SlippiConnection.cs


注:本文中的NetworkStream.ReadExactBytesAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。