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


C# TcpListener.?.Stop方法代码示例

本文整理汇总了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());
        }
      }
    }
开发者ID:VelocityDB,项目名称:VelocityDB,代码行数:66,代码来源:Service.cs

示例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();
            }
        }
开发者ID:mariog86,项目名称:PointOfSale,代码行数:32,代码来源:PointOfSaleTest.cs


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