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


C# NetworkStream.FlushAsync方法代码示例

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


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

示例1: SendTransportPacket

 public static async Task SendTransportPacket(NetworkStream sw, TransportPacketType type, byte[] Data)
 {
     TransportPacket tp = new TransportPacket(Data, type, Constants.TransportVersion);
     byte[] msg = PacketCodec.CreateTransportPacket(tp);
     sw.Write(msg, 0, msg.Length);
     await sw.FlushAsync();
 }
开发者ID:dipamchang,项目名称:tnetd-open,代码行数:7,代码来源:PacketSender.cs

示例2: Sender

 public static async Task Sender(NetworkStream stream, CancellationTokenSource cts)
 {
     WriteLine("Sender task");
     while (true)
     {
         WriteLine("enter a string to send, shutdown to exit");
         string line = ReadLine();
         byte[] buffer = Encoding.UTF8.GetBytes($"{line}\r\n");
         await stream.WriteAsync(buffer, 0, buffer.Length);
         await stream.FlushAsync();
         if (string.Compare(line, "shutdown", ignoreCase: true) == 0)
         {
             cts.Cancel();
             WriteLine("sender task closes");
             break;
         }
     }
 }
开发者ID:ProfessionalCSharp,项目名称:ProfessionalCSharp6,代码行数:18,代码来源:Program.cs

示例3: SendAsync

 /// <summary>
 /// Asynchronously sends a buffer(packet) through the specified stream.
 /// </summary>
 /// <param name="stream">The particular stream of a TCP client to send through.</param>
 /// <param name="buffer">Buffer containing the packet to be sent.</param>
 /// <exception cref="System.ArugmentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.ObjectDisposedException"/>
 public static async void SendAsync( NetworkStream stream, BufferStream buffer ) {
     stream.Write( buffer.Memory, 0 , buffer.Iterator );
     await stream.FlushAsync();
 }
开发者ID:FatalSleep,项目名称:Sharp-Server,代码行数:13,代码来源:PacketStream.cs

示例4: ConnectAsync

        public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken)
        {
            _state = WebSocketState.Connecting;

            await _connection.ConnectAsync(uri.Host, uri.Port);

            _stream = _connection.GetStream();

            var secKey = Convert.ToBase64String(Encoding.ASCII.GetBytes(Guid.NewGuid().ToString().Substring(0, 16)));
            var expectedAccept = HandshakeHelpers.CreateResponseKey(secKey);

            var headerString =
                $"GET {uri.PathAndQuery} HTTP/1.1\r\n" +
                $"Host: {uri.Host}\r\n" +
                "Connection: Upgrade\r\n" +
                "Upgrade: websocket\r\n" +
                "Sec-WebSocket-Version: 13\r\n" +
                $"Sec-WebSocket-Protocol: {_subProtocol}\r\n" +
                $"Sec-WebSocket-Key: {secKey}\r\n\r\n";

            var bytes = Encoding.UTF8.GetBytes(headerString);
            await _stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
            await _stream.FlushAsync(cancellationToken);

            var buffer = new byte[1024];
            var resultLenth = await _stream.ReadAsync(buffer, 0, 1024, cancellationToken);
            var resultString = new StringReader(Encoding.UTF8.GetString(buffer, 0, resultLenth));

            var respCode = 0;
            var headers = new Dictionary<string, string>();
            var line = resultString.ReadLine();
            while (line != null)
            {
                if (line.StartsWith("HTTP/1.1 ") && line.Length > 11)
                    respCode = Convert.ToInt16(line.Substring(9, 3));
                else
                {
                    var items = line.Split(new[] { ':' }, 2);
                    if (items.Length == 2)
                        headers[items[0]] = items[1].TrimStart();
                }

                line = resultString.ReadLine();
            }

            if (respCode != (int) HttpStatusCode.SwitchingProtocols)
            {
                throw new WebSocketException($"The server returned status code '{respCode}' when status code '101' was expected");
            }

            if (!string.Equals(headers["Upgrade"], "WebSocket", StringComparison.OrdinalIgnoreCase)
                || !string.Equals(headers["Connection"], "Upgrade", StringComparison.OrdinalIgnoreCase)
                || !string.Equals(headers["Sec-WebSocket-Accept"], expectedAccept))
            {
                throw new WebSocketException("HTTP header error during handshake");
            }

            _state = WebSocketState.Open;

            if (_keepAliveInterval != Timeout.InfiniteTimeSpan)
            {
                _keepAliveTimer = new Timer(SendKeepAlive, this, _keepAliveInterval, _keepAliveInterval);
            }
        }
开发者ID:AdaptiveConsulting,项目名称:ReactiveTraderCloud,代码行数:64,代码来源:CommonWebSocket.cs


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