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


C# Socket.SendToAsync方法代码示例

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


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

示例1: DualModeSendToAsync_IPEndPointToHost_Helper

        private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false)
        {
            int port;
            Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp);
            using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port))
            {
                // Send a few packets, in case they aren't delivered reliably.
                for (int i = 0; i < Configuration.UDPRedundancy; i++)
                {
                    using (ManualResetEvent waitHandle = new ManualResetEvent(false))
                    {
                        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                        args.RemoteEndPoint = new IPEndPoint(connectTo, port);
                        args.SetBuffer(new byte[1], 0, 1);
                        args.UserToken = waitHandle;
                        args.Completed += AsyncCompleted;

                        bool async = client.SendToAsync(args);
                        if (async)
                        {
                            Assert.True(waitHandle.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");
                        }

                        Assert.Equal(1, args.BytesTransferred);
                        if (args.SocketError != SocketError.Success)
                        {
                            throw new SocketException((int)args.SocketError);
                        }
                    }
                }

                bool success = server.WaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout); // Make sure the bytes were received
                if (!success)
                {
                    throw new TimeoutException();
                }
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:38,代码来源:DualModeSocketTest.cs

示例2: ClientSend

            private void ClientSend(object state)
            {
                try
                {
                    Socket socket = new Socket(_connectTo.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

                    for (int i = 0; i < Configuration.UDPRedundancy; i++)
                    {
                        SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                        e.RemoteEndPoint = new IPEndPoint(_connectTo, _port);
                        e.SetBuffer(new byte[1], 0, 1);

                        socket.SendToAsync(e);
                    }
                }
                catch (SocketException)
                {
                    _serverSocket.Dispose(); // Cancels the test
                }
            }
开发者ID:eerhardt,项目名称:corefx,代码行数:20,代码来源:DualModeSocketTest.cs

示例3: Socket_SendToAsyncDnsEndPoint_Throws

 [Fact] // Base case
 // "The parameter remoteEP must not be of type DnsEndPoint."
 public void Socket_SendToAsyncDnsEndPoint_Throws()
 {
     Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
     SocketAsyncEventArgs args = new SocketAsyncEventArgs();
     args.RemoteEndPoint = new DnsEndPoint("localhost", UnusedPort);
     args.SetBuffer(new byte[1], 0, 1);
     Assert.Throws<ArgumentException>(() =>
     {
         socket.SendToAsync(args);
     });
 }
开发者ID:eerhardt,项目名称:corefx,代码行数:13,代码来源:DualModeSocketTest.cs

示例4: Socket_SendToAsyncV4IPEndPointToV4Host_Throws

        [Fact] // Base case
        public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws()
        {
            Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
            args.SetBuffer(new byte[1], 0, 1);
            bool async = socket.SendToAsync(args);
            Assert.False(async);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Assert.Equal(SocketError.Fault, args.SocketError);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // NOTE: on Linux, this API returns ENETUNREACH instead of EFAULT: this platform
                //       checks the family of the provided socket address before checking its size
                //       (as long as the socket address is large enough to store an address family).
                Assert.Equal(SocketError.NetworkUnreachable, args.SocketError);
            }
            else
            {
                // NOTE: on other Unix platforms, this API returns EINVAL instead of EFAULT.
                Assert.Equal(SocketError.InvalidArgument, args.SocketError);
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:27,代码来源:DualModeSocketTest.cs

示例5: SendToRecvFromAsync_Datagram_UDP


//.........这里部分代码省略.........
            var receivedChecksums = new uint?[DatagramsToSend];
            var receiveBuffer = new byte[DatagramSize];
            int receivedDatagrams = -1;

            Action<int, EndPoint> receiveHandler = null;
            receiveHandler = (received, remote) =>
            {
                try
                {
                    if (receivedDatagrams != -1)
                    {
                        Assert.Equal(DatagramSize, received);
                        Assert.Equal(rightEndpoint, remote);

                        int datagramId = (int)receiveBuffer[0];
                        Assert.Null(receivedChecksums[datagramId]);

                        receivedChecksums[datagramId] = Fletcher32.Checksum(receiveBuffer, 0, received);

                        receiverAck.Set();
                        Assert.True(senderAck.Wait(AckTimeout));
                        senderAck.Reset();

                        receivedDatagrams++;
                        if (receivedDatagrams == DatagramsToSend)
                        {
                            left.Dispose();
                            receiverFinished.SetResult(true);
                            return;
                        }
                    }
                    else
                    {
                        receivedDatagrams = 0;
                    }

                    left.ReceiveFromAsync(leftEventArgs, receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, receiveRemote, receiveHandler);
                }
                catch (Exception ex)
                {
                    receiverFinished.SetException(ex);
                }
            };

            receiveHandler(0, null);

            var random = new Random();
            var senderFinished = new TaskCompletionSource<bool>();
            var sentChecksums = new uint[DatagramsToSend];
            var sendBuffer = new byte[DatagramSize];
            int sentDatagrams = -1;

            Action<int> sendHandler = null;
            sendHandler = sent =>
            {
                try
                {
                    if (sentDatagrams != -1)
                    {
                        Assert.True(receiverAck.Wait(AckTimeout));
                        receiverAck.Reset();
                        senderAck.Set();

                        Assert.Equal(DatagramSize, sent);
                        sentChecksums[sentDatagrams] = Fletcher32.Checksum(sendBuffer, 0, sent);

                        sentDatagrams++;
                        if (sentDatagrams == DatagramsToSend)
                        {
                            right.Dispose();
                            senderFinished.SetResult(true);
                            return;
                        }
                    }
                    else
                    {
                        sentDatagrams = 0;
                    }

                    random.NextBytes(sendBuffer);
                    sendBuffer[0] = (byte)sentDatagrams;
                    right.SendToAsync(rightEventArgs, sendBuffer, 0, sendBuffer.Length, SocketFlags.None, leftEndpoint, sendHandler);
                }
                catch (Exception ex)
                {
                    senderFinished.SetException(ex);
                }
            };

            sendHandler(0);

            Assert.True(receiverFinished.Task.Wait(TestTimeout));
            Assert.True(senderFinished.Task.Wait(TestTimeout));

            for (int i = 0; i < DatagramsToSend; i++)
            {
                Assert.NotNull(receivedChecksums[i]);
                Assert.Equal(sentChecksums[i], (uint)receivedChecksums[i]);
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:101,代码来源:SendReceive.cs

示例6: Socket_SendToAsyncV4IPEndPointToV4Host_Throws

        public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws()
        {
            Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
            socket.DualMode = false;

            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
            args.SetBuffer(new byte[1], 0, 1);

            bool async = socket.SendToAsync(args);
            Assert.False(async);
            Assert.Equal(SocketError.Fault, args.SocketError);
        }
开发者ID:rainersigwald,项目名称:corefx,代码行数:13,代码来源:DualModeSocketTest.cs

示例7: DualModeSendToAsync_IPEndPointToHost_Helper

        private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
        {
            int port;
            ManualResetEvent waitHandle = new ManualResetEvent(false);
            Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp);
            using (SocketUdpServer server = new SocketUdpServer(listenOn, dualModeServer, out port))
            {
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = new IPEndPoint(connectTo, port);
                args.SetBuffer(new byte[1], 0, 1);
                args.UserToken = waitHandle;
                args.Completed += AsyncCompleted;

                bool async = client.SendToAsync(args);
                if (async)
                {
                    Assert.True(waitHandle.WaitOne(5000), "Timeout while waiting for connection");
                }

                Assert.Equal(1, args.BytesTransferred);
                if (args.SocketError != SocketError.Success)
                {
                    throw new SocketException((int)args.SocketError);
                }

                bool success = server.WaitHandle.WaitOne(100); // Make sure the bytes were received
                if (!success)
                {
                    throw new TimeoutException();
                }
            }
        }
开发者ID:nnyamhon,项目名称:corefx,代码行数:32,代码来源:DualModeSocketTest.cs

示例8: Socket_SendToAsyncV4IPEndPointToV4Host_Throws_Unix

 public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws_Unix()
 {
     Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
     SocketAsyncEventArgs args = new SocketAsyncEventArgs();
     args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
     args.SetBuffer(new byte[1], 0, 1);
     bool async = socket.SendToAsync(args);
     Assert.False(async);
     Assert.Equal(SocketError.NetworkUnreachable, args.SocketError);
 }
开发者ID:Highsong,项目名称:corefx,代码行数:10,代码来源:DualModeSocketTest.cs


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