System.Net.Sockets.Socket.Bind Method

Associates a System.Net.Sockets.Socket with a local endpoint.

Syntax

public void Bind (System.Net.EndPoint local_end)

Parameters

local_end
The local System.Net.EndPoint to be associated with the socket.

Exceptions

TypeReason
ArgumentNullException localEP is null.
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
ObjectDisposedExceptionThe current instance has been disposed.
System.Security.SecurityException A caller in the call stack does not have the required permission.

Remarks

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

Before calling Socket.Bind(System.Net.EndPoint), you must first create the local System.Net.IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an System.Net.IPEndPoint using System.Net.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 System.Net.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 Socket.LocalEndPoint. If you are using a connection-oriented protocol, Socket.LocalEndPoint will not return the locally assigned network address until after you have made a call to the Socket.Connect(System.Net.EndPoint) or Socket.EndConnect(IAsyncResult) method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.

If a UDP socket wants to receive interface information on received packets, the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method should be explicitly called with the socket option set to SocketOptionName.PacketInformation immediately after calling the Socket.Bind(System.Net.EndPoint) method.

Note:

If you intend to receive multicast datagrams, you must call the Socket.Bind(System.Net.EndPoint) method with a multicast port number.

Note:

You must call the Socket.Bind(System.Net.EndPoint) method if you intend to receive connectionless datagrams using the Socket.ReceiveFrom(Byte[], int, int, SocketFlags, System.Net.EndPoint@) method.

Note:

If you receive a System.Net.Sockets.SocketException when calling the Socket.Bind(System.Net.EndPoint) 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:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Permissions

TypeReason
System.Net.SocketPermissionRequires permission to accept connections on the endpoint defined by localEP. See System.Net.NetworkAccess.Accept.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0