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


C# Socket.EndAccept方法代码示例

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


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

示例1: ReceiveTimesOut_Throws

        public void ReceiveTimesOut_Throws()
        {
            using (Socket localSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
            {
                using (Socket remoteSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
                {
                    int port = localSocket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                    localSocket.Listen(1);
                    IAsyncResult localAsync = localSocket.BeginAccept(null, null);

                    remoteSocket.Connect(IPAddress.IPv6Loopback, port);

                    Socket acceptedSocket = localSocket.EndAccept(localAsync);
                    acceptedSocket.ReceiveTimeout = 100;

                    SocketException sockEx = Assert.Throws<SocketException>(() =>
                   {
                       acceptedSocket.Receive(new byte[1]);
                   });

                    Assert.Equal(SocketError.TimedOut, sockEx.SocketErrorCode);
                    Assert.True(acceptedSocket.Connected);
                }
            }
        }
开发者ID:fuwei199006,项目名称:corefx,代码行数:25,代码来源:TimeoutTest.cs

示例2: AcceptCallback

    public static void AcceptCallback(IAsyncResult ar)
    {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        connectSocket = (Socket) ar.AsyncState;
        connectSocket = connectSocket.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = connectSocket;
        connectSocket.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
        Send(connectSocket, "Hello!");
        Debug.Log("[ROTATION_SERVER] Sent INIT sequence to client.");
    }
开发者ID:stefanseibert,项目名称:padawan101,代码行数:16,代码来源:RotationServer.cs

示例3: SocketSendTimeout_Send_Success

        public void SocketSendTimeout_Send_Success()
        {
            using (Socket localSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
            {
                using (Socket remoteSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
                {
                    int port = localSocket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                    localSocket.Listen(1);
                    IAsyncResult localAsync = localSocket.BeginAccept(null, null);

                    remoteSocket.Connect(IPAddress.IPv6Loopback, port);

                    Socket acceptedSocket = localSocket.EndAccept(localAsync);
                    acceptedSocket.SendTimeout = 100;

                    // Note that Send almost never times out because it only has to copy the data to the native buffer.
                    int bytes = acceptedSocket.Send(new byte[100]);
                    Assert.Equal(100, bytes);
                    Assert.True(acceptedSocket.Connected);
                }
            }
        }
开发者ID:fuwei199006,项目名称:corefx,代码行数:22,代码来源:TimeoutTest.cs

示例4: DualModeConnect_BeginAccept_Helper

        private void DualModeConnect_BeginAccept_Helper(IPAddress listenOn, IPAddress connectTo)
        {
            using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
            {
                int port = serverSocket.BindToAnonymousPort(listenOn);
                serverSocket.Listen(1);
                IAsyncResult async = serverSocket.BeginAccept(null, null);
                SocketClient client = new SocketClient(_log, serverSocket, connectTo, port);

                // Due to the nondeterministic nature of calling dispose on a Socket that is doing
                // an EndAccept operation, we expect two types of exceptions to happen.
                Socket clientSocket;
                try
                {
                    clientSocket = serverSocket.EndAccept(async);
                    Assert.True(clientSocket.Connected);
                    AssertDualModeEnabled(clientSocket, listenOn);
                    Assert.Equal(AddressFamily.InterNetworkV6, clientSocket.AddressFamily);
                    if (connectTo == IPAddress.Loopback)
                    {
                        Assert.Contains(((IPEndPoint)clientSocket.LocalEndPoint).Address, ValidIPv6Loopbacks);
                    }
                    else
                    {
                        Assert.Equal(connectTo.MapToIPv6(), ((IPEndPoint)clientSocket.LocalEndPoint).Address);
                    }
                    Assert.Equal(connectTo.MapToIPv6(), ((IPEndPoint)clientSocket.LocalEndPoint).Address);
                }
                catch (ObjectDisposedException) { }
                catch (SocketException) { }

                Assert.True(
                    client.WaitHandle.WaitOne(Configuration.PassingTestTimeout),
                    "Timed out while waiting for connection");

                if ( client.Error != SocketError.Success)
                {
                    throw new SocketException((int)client.Error);
                }
            }
        }
开发者ID:eerhardt,项目名称:corefx,代码行数:41,代码来源:DualModeSocketTest.cs

示例5: DualModeConnect_BeginAccept_Helper

 private void DualModeConnect_BeginAccept_Helper(IPAddress listenOn, IPAddress connectTo)
 {
     using (Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp))
     {
         int port = serverSocket.BindToAnonymousPort(listenOn);
         serverSocket.Listen(1);
         IAsyncResult async = serverSocket.BeginAccept(null, null);
         SocketClient client = new SocketClient(serverSocket, connectTo, port);
         Socket clientSocket = serverSocket.EndAccept(async);
         Assert.True(clientSocket.Connected);
         Assert.True(clientSocket.DualMode);
         Assert.Equal(AddressFamily.InterNetworkV6, clientSocket.AddressFamily);
         Assert.Equal(connectTo.MapToIPv6(), ((IPEndPoint)clientSocket.LocalEndPoint).Address);
     }
 }
开发者ID:rainersigwald,项目名称:corefx,代码行数:15,代码来源:DualModeSocketTest.cs


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