本文整理汇总了C#中System.Net.Sockets.TcpListener.?.Stop方法的典型用法代码示例。如果您正苦于以下问题:C# TcpListener.?.Stop方法的具体用法?C# TcpListener.?.Stop怎么用?C# TcpListener.?.Stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.TcpListener
的用法示例。
在下文中一共展示了TcpListener.?.Stop方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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());
}
}
}
示例2: ReadFromClient
private static string ReadFromClient()
{
TcpListener listener = null;
try
{
listener = new TcpListener(IPAddress.Any, 6740);
listener.Start();
var acceptTcpClientAsync = listener.AcceptTcpClientAsync();
acceptTcpClientAsync.Wait(5000);
if (acceptTcpClientAsync.IsCompleted)
{
using (TcpClient client = acceptTcpClientAsync.Result)
{
using (StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8))
{
string actual;
actual = reader.ReadLine();
return actual;
}
}
}
else
{
return null;
}
}
finally
{
listener?.Stop();
}
}