示例#1
0
        public int Start()
        {
            if (m_listening)
            {
                return(0);
            }
            var address = !string.IsNullOrEmpty(m_address) ? IPAddress.Parse(m_address) : IPAddress.Any;

            m_server = new NtTcpListener(address, m_port);

            try
            {
                m_server.Start(5);
            }
            catch (ObjectDisposedException)
            {
                return(1);
            }
            catch (SocketException ex)
            {
                Error(Logger.Instance, $"TcpListener Start(): failed {ex.SocketErrorCode}");
                return((int)ex.SocketErrorCode);
            }

            m_listening = true;
            return(0);
        }
示例#2
0
        public void Shutdown()
        {
            m_shutdown = true;

            //Force wakeup with non-blocking connect to ourselves
            var address = !string.IsNullOrEmpty(m_address) ? IPAddress.Parse(m_address) : IPAddress.Loopback;

            Socket connectSocket;

            try
            {
                connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            catch (SocketException)
            {
                return;
            }

            connectSocket.Blocking = false;

            try
            {
                connectSocket.Connect(address, m_port);
                connectSocket.Dispose();
            }
            catch (SocketException)
            {
            }

            m_listening = false;
            m_server?.Stop();
            m_server = null;



            m_listening = false;
            m_server?.Stop();
            m_server = null;
        }