本文整理汇总了C#中System.Net.Sockets.TcpListener.?.BeginAcceptTcpClient方法的典型用法代码示例。如果您正苦于以下问题:C# TcpListener.?.BeginAcceptTcpClient方法的具体用法?C# TcpListener.?.BeginAcceptTcpClient怎么用?C# TcpListener.?.BeginAcceptTcpClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.TcpListener
的用法示例。
在下文中一共展示了TcpListener.?.BeginAcceptTcpClient方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: tcpListen
protected static void tcpListen()
{
try
{
//IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
//listener = new TcpListener(ipAddress, ServerTcpClient.odbTcpPort);
//listener = new TcpListener(IPAddress.IPv6Any, ServerTcpClient.odbTcpPort);
listener = new TcpListener(IPAddress.Any, SessionBase.s_serverTcpIpPortNumber);
Socket s = listener.Server;
LingerOption lingerOption = new LingerOption(true, 0);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
listener.Start();
}
catch (System.Exception e)
{
ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
}
try
{
while (!ServerTcpClient.ShutDown)
{
ServerTcpClient.s_acceptDone.Reset();
#if NET_CORE
//listener?.AcceptSocketAsync().Wait();
ServerTcpClient.AcceptTcpClient(listener);
#else
listener?.BeginAcceptTcpClient(new AsyncCallback(ServerTcpClient.AcceptTcpClient), listener);
#endif
ServerTcpClient.s_acceptDone.WaitOne();
}
}
catch (SocketException e)
{
#if !NET_CORE
if (e.ErrorCode != 10054) // client closed socket
#endif
{
ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
}
}
catch (System.Exception e)
{
ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
}
finally
{
listener.Stop();
}
if (!stopService)
{
try
{
listener?.Stop();
Environment.Exit(0);
}
catch (System.Exception e)
{
ServerTcpClient.s_odbServerLog.WriteEntry(e.ToString());
}
}
}