This documentation is archived and is not being maintained.

Socket.Bind Method

Associates a Socket with a local endpoint.

[Visual Basic]
Public Sub Bind( _
   ByVal localEP As EndPoint _
public void Bind(
 EndPoint localEP
public: void Bind(
 EndPoint* localEP
public function Bind(
   localEP : EndPoint


The local EndPoint to associate with the Socket.


Exception Type Condition
ArgumentNullException localEP is a null reference (Nothing in Visual Basic).
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   If you intend to receive multicasted datagrams, you must call the Bind method with a multicast port number.
Note   You must call the Bind method if you intend to receive connectionless datagrams using the ReceiveFrom method.
Note   If you receive a SocketException when calling the Bind method, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.


[Visual Basic, C#, C++] The following example binds a Socket using the specified local endpoint.

[Visual Basic] 
Catch e As Exception
    Console.WriteLine("Winsock error: " & e.ToString())
End Try

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

try {
catch (Exception* e) {
    Console::WriteLine(S"Winsock error: {0}", e);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | IPEndPoint | Connect | Listen | IPAddress | LocalEndPoint