本文整理汇总了C#中Socket.DisconnectAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.DisconnectAsync方法的具体用法?C# Socket.DisconnectAsync怎么用?C# Socket.DisconnectAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.DisconnectAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DisconnectAsync_Success
public void DisconnectAsync_Success()
{
AutoResetEvent completed = new AutoResetEvent(false);
IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnCompleted;
args.UserToken = completed;
args.RemoteEndPoint = server1.EndPoint;
args.DisconnectReuseSocket = true;
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.True(client.ConnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
Assert.True(client.DisconnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
args.RemoteEndPoint = server2.EndPoint;
Assert.True(client.ConnectAsync(args));
completed.WaitOne();
Assert.Equal(SocketError.Success, args.SocketError);
}
}
}
示例2: Success
public void Success()
{
AutoResetEvent completed = new AutoResetEvent(false);
if (Socket.OSSupportsIPv4)
{
using (SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase)))
using (SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase + 1)))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnCompleted;
args.UserToken = completed;
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, TestPortBase);
args.DisconnectReuseSocket = true;
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Assert.True(client.ConnectAsync(args));
Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
Assert.True(client.DisconnectAsync(args));
Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, TestPortBase + 1);
Assert.True(client.ConnectAsync(args));
Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
client.Dispose();
}
}
}
示例3: DisconnectAsync_NonWindows_NotSupported
public void DisconnectAsync_NonWindows_NotSupported()
{
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.DisconnectReuseSocket = true;
Assert.Throws<PlatformNotSupportedException>(() => client.DisconnectAsync(args));
}
}
示例4: DisconnectAsync_Throws_PlatformNotSupported
public void DisconnectAsync_Throws_PlatformNotSupported()
{
IPAddress address = null;
if (Socket.OSSupportsIPv4)
{
address = IPAddress.Loopback;
}
else if (Socket.OSSupportsIPv6)
{
address = IPAddress.IPv6Loopback;
}
else
{
return;
}
int port;
using (SocketTestServer.SocketTestServerFactory(address, out port))
{
var completed = new AutoResetEvent(false);
var args = new SocketAsyncEventArgs
{
UserToken = completed,
RemoteEndPoint = new IPEndPoint(address, port),
DisconnectReuseSocket = true
};
args.Completed += OnCompleted;
var client = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Assert.True(client.ConnectAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
Assert.Equal<SocketError>(SocketError.Success, args.SocketError);
Assert.Throws<PlatformNotSupportedException>(() => client.DisconnectAsync(args));
client.Dispose();
}
}