コード例 #1
0
        public void Bind(IPAddress ipAddress, ushort ipPort)
        {
            if (_sourceIpAddressAndPortAssigned)
            {
                throw new SocketException("Socket is connected.");
            }

            _sourceIpAddressAndPortAssigned = true;

            // if ipAddress is IP_ADDRESS_ANY, then change it to to our actual ipAddress.
            if (ipAddress.Equals(IPv6Any))
            {
                sourceIpAddress = NetworkInterface.IPAddress;
            }
            else
            {
                sourceIpAddress = ipAddress;
            }

            if (ipPort == IPPortAny)
            {
                sourcePort = NetworkInterface.GetEphemeralPort();
            }
            else
            {
                sourcePort = ipPort;
            }


            // verify that this source IP address is correct
            if (sourceIpAddress != NetworkInterface.IPAddress)
            {
                throw new SocketException("Source address is not correct.");
            }

            NetworkInterface.CreateSocket(this);
            ReceiveTimeout = 5000;
        }