Пример #1
0
        /// <summary>
        /// Initialize a new instance of the <see cref="TcpListener"/> class.
        /// </summary>
        /// <param name="port">
        /// It's the local port where the connection requests will be listened.
        /// </param>
        public TcpListener(int port)
        {
            _localInterface = "localhost";

            if (!NetUtilities.IsValidTcpPort(port))
            {
                throw new ArgumentOutOfRangeException("port");
            }

            _port = port;
        }
Пример #2
0
        /// <summary>
        /// Initialize a new instance of the <see cref="TcpListener"/> class,
        /// ready to listen requests over a given IP address and port.
        /// </summary>
        /// <param name="localAddress">
        /// It's the local address where the connection requests will be listened.
        /// </param>
        /// <param name="port">
        /// It's the local port where the connection requests will be listened.
        /// </param>
        public TcpListener(IPAddress localAddress, int port)
        {
            if (localAddress == null)
            {
                throw new ArgumentNullException("localAddress");
            }

            if (!NetUtilities.IsValidTcpPort(port))
            {
                throw new ArgumentOutOfRangeException("port");
            }

            CreateServerSocket(new IPEndPoint(localAddress, port));
        }
Пример #3
0
        /// <summary>
        /// Start to listen connection requests.
        /// </summary>
        public void Start()
        {
            lock (this) {
                if (_listening)
                {
                    return;
                }

                if (_localEndPoint == null)
                {
                    // Local end point unspecified, try to solve it trought
                    // Port and LocalInterface properties.
                    if (_localInterface == null)
                    {
                        throw new MessagingException("Invalid local interface.");
                    }

                    if (!NetUtilities.IsValidTcpPort(_port))
                    {
                        throw new MessagingException("Invalid port number.");
                    }

#if NET20
                    IPHostEntry hostEntry = null;
                    IPAddress   addr      = null;
                    if (IPAddress.TryParse(_localInterface, out addr))
                    {
                        // Create server socket, over the first located address.
                        _localEndPoint = new IPEndPoint(addr, _port);
                    }
                    else
                    {
                        hostEntry = Dns.GetHostEntry(_localInterface);

                        if (hostEntry.AddressList.Length == 0)
                        {
                            throw new MessagingException("Can't resolve local interface name.");
                        }

                        // Create server socket, over the first located address.
                        _localEndPoint = new IPEndPoint(hostEntry.AddressList[0], _port);
                    }
#else
                    IPHostEntry hostEntry = Dns.Resolve(_localInterface);

                    if (hostEntry.AddressList.Length == 0)
                    {
                        throw new MessagingException("Can't resolve local interface name.");
                    }

                    // Create server socket, over the first located address.
                    _localEndPoint = new IPEndPoint(hostEntry.AddressList[0], _port);
#endif
                }

                if (_socket == null)
                {
                    CreateServerSocket(_localEndPoint);
                }

                // Bind and start listening.
                _socket.Bind(_localEndPoint);
                _socket.Listen(10);

                _listening = true;

                // Start an asynchronous Accept operation.
                _socket.BeginAccept(new AsyncCallback(AsyncAcceptRequestHandler), _socket);
            }
        }