Пример #1
0
        /// <inheritdoc />
        public void Bind(IPAddress localAddress)
        {
            IPEndPoint ep;

            _serverSocket  = Socket2.CreateSocketAndBindToAnyPort(localAddress, out ep);
            _localEndPoint = ep;
            Listen();
        }
Пример #2
0
        public void TestCreateSocketAndBindToAnyPort3()
        {
            IPEndPoint address;

            using (var socket = Socket2.CreateSocketAndBindToAnyPort(IPAddress.Any, out address))
            {
                socket.ExclusiveAddressUse.Should().BeTrue();
            }
        }
Пример #3
0
        public void TestCreateSocketAndBindToAnyPort2()
        {
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                const ushort usedPort = 55555;
                socket.Bind(new IPEndPoint(IPAddress.Loopback, usedPort));

                IPEndPoint address;
                new Action(() =>
                           Socket2.CreateSocketAndBindToAnyPort(IPAddress.Any, usedPort, usedPort,
                                                                out address)
                           )
                .Should().Throw <SystemException>()
                .WithMessage("No more available sockets");
            }
        }
Пример #4
0
        /// <summary>
        ///     Binds this socket to the given address.
        ///     The listening port will in the range of [<paramref name="minPort"/>, <paramref name="maxPort"/>] and can be retrieved
        ///     via <see cref="LocalEndPoint"/> after this call has succeeded.
        /// </summary>
        /// <remarks>
        ///     The current implementation tries to bind the socket to <paramref name="minPort"/> and then increments
        ///     it by one until either a Bind() operation succeeds or maxPort has been reached. If the latter occured
        ///     (and Bind() still didn't succeed, then a <see cref="SystemException"/> is thrown.
        /// </remarks>
        /// <param name="localAddress"></param>
        /// <param name="minPort">The minimum port number to which this endpoint may be bound to</param>
        /// <param name="maxPort">The maximum port number to which this endpoint may be bound to</param>
        /// <exception cref="SystemException">When none of the given ports is available</exception>
        public void Bind(IPAddress localAddress, ushort minPort, ushort maxPort)
        {
            if (localAddress == null)
            {
                throw new ArgumentNullException(nameof(localAddress));
            }
            if (IsConnected)
            {
                throw new InvalidOperationException("A socket may only bound to a particular port when its not already connected");
            }

            IPEndPoint ep;

            _serverSocket = Socket2.CreateSocketAndBindToAnyPort(localAddress, minPort, maxPort, out ep);
            LocalEndPoint = ep;
            Listen();
        }