Share via


Socket.Bind Method

Associates a socket with a local endpoint.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

Syntax

public void Bind (
         EndPointlocalEP
)

Parameters

  • localEP
    An EndPoint object that specifies the local endpoint.

Remarks

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen method. You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint. You can use the Bind method on both connection-oriented and connectionless protocols.

Before calling Bind, you must first create an IPEndPoint object to represent the local endpoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint object using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an IPEndPoint object using 0 (zero) as the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

If you use the preceding approach, you can discover what local network address and port number have been assigned by using the LocalEndPoint property. If you are using a connection-oriented protocol, LocalEndPoint will not return the locally assigned network address until after you have made a call to the Connect method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive operation.

Version Information

Available in .NET Micro Framework version 2.0, 2.5

See Also

Reference

Socket Class
Socket Members
System.Net.Sockets Namespace