This topic has not yet been rated - Rate this topic

Socket.Bind Method

Associates a Socket with a local endpoint.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public void Bind(
	EndPoint localEP
)

Parameters

localEP
Type: System.Net.EndPoint
The local EndPoint to associate with the Socket.
Exception Condition
ArgumentNullException

localEP is null.

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

ObjectDisposedException

The Socket has been closed.

SecurityException

A caller higher in the call stack does not have permission for the requested operation.

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 connectionless and connection-oriented protocols.

Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint 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 using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

If you use the above approach, you can discover what local network address and port number has been assigned by calling the LocalEndPoint. 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 or EndConnect method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.

Note Note

If you intend to receive multicast datagrams, you must call the Bind method with a multicast port number.

Note Note

You must call the Bind method if you intend to receive connectionless datagrams using the ReceiveFrom method.

Note Note

If you receive a SocketException when calling the Bind method, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following code example binds a Socket using the specified local endpoint.


try {
    aSocket.Bind(anEndPoint);
}
catch (Exception e) {
    Console.WriteLine("Winsock error: " + e.ToString());
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • SocketPermission  

    for accepting connections from the host defined by localEP. Associated enumeration: NetworkAccess.Accept

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Multicast datagrams note is wrong
The statment:
"If you intend to receive multicast datagrams, you must call the Bind method with a multicast port number."
should read:
"If you intend to receive multicast datagrams, you must call the Bind method with a multicast IP address."

There is no such thing as a "multicast port number".
Ephemeral port range was updated in Windows 7 and 2008
According to the Microsoft Support article 'The default dynamic port range for TCP/IP has changed in Windows Vista and in Windows Server 2008', http://support.microsoft.com/kb/929851/, the ephemeral port range for these operating systems is 49152 - 65535.
Assigned Port
The documentation appears to be wrong on what port it will assign.

It is assigning ports well above 5000 for me.  The last port I was assigned was 57970.

I think the range would be 1024 to 65535.