System.Net.Sockets.Socket.Bind C# (CSharp) Method

Bind() public method

public Bind ( EndPoint localEP ) : void
localEP System.Net.EndPoint
return void
        public void Bind(EndPoint localEP)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, localEP);

            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (localEP == null)
            {
                throw new ArgumentNullException(nameof(localEP));
            }

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"localEP:{localEP}");
            EndPoint endPointSnapshot = localEP;
            IPEndPoint ipSnapshot = localEP as IPEndPoint;

            // For now security is implemented only on IPEndPoint.
            // If EndPoint is of any other type, unmanaged code permission is demanded.
            if (ipSnapshot != null)
            {
                // Take a snapshot that will make it immutable and not derived.
                ipSnapshot = ipSnapshot.Snapshot();
                endPointSnapshot = RemapIPEndPoint(ipSnapshot);

                // NB: if local port is 0, then winsock will assign some port > 1024,
                //     which is assumed to be safe.
            }

            // Ask the EndPoint to generate a SocketAddress that we can pass down to native code.
            Internals.SocketAddress socketAddress = CallSerializeCheckDnsEndPoint(endPointSnapshot);
            DoBind(endPointSnapshot, socketAddress);
            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Usage Example

        public WebServer()
        {
            greenThread = new Thread(green.LightControl);
            greenThread.Start();
            amberThread = new Thread(amber.LightControl);
            amberThread.Start();
            redThread = new Thread(red.LightControl);
            redThread.Start();
            //Initialize Socket class
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //Request and bind to an IP from DHCP server
            socket.Bind(new IPEndPoint(IPAddress.Any, 80));
            string address =
                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress;
            //Debug print our IP address
            while (address == "192.168.5.100")
            {
                address =
                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress;
                Thread.Sleep(2000);
            }
            Debug.Print(address);
            //Start listen for web requests
            socket.Listen(10);

            ListenForRequest();
        }
All Usage Examples Of System.Net.Sockets.Socket::Bind