本文整理汇总了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);
}
}
}
示例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.");
}
示例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);
}
}
}
示例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);
}
}
}
示例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);
}
}