Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
bind (ws2_32)
The bind function associates a local address with a socket.
The bind function is used on an unconnected socket before subsequent calls to the connect or listen functions. It is not neccessary to use bind for connect(), but is required for listen(). This binding process is performed by the connect() function implicitly.
socketAddress A sockaddr_in structure used to identify the IP address and port to bind to.
addressLength The length of the supplied socketAddress parameter.

    public static extern int bind(IntPtr s, ref sockaddr_in addr, int addrsize);

    public static extern int bind(IntPtr s, ref sockaddr_in6 addr, int addrsize);

VB Signature:

Declare Function bind Lib "ws2_32.dll" ( _
        socketHandle As IntPtr, _
        ByRef socketAddress As sockaddr_in, _
        addressLength as Integer) As Integer

User-Defined Types:

/// <summary>
/// Internet socket address structure.
/// </summary>

Alternative Managed API:

Do you know one? Please contribute it!



Tips & Tricks:

Please add some!

Sample Code:

public static bool Bind(string ipAddress, int port, IntPtr socketHandle)
    sockaddr_in remoteAddress;            //Remote address structure.
    int resultCode = 0;            //Result of operation.
    int errorCode = 0;                //Resultant error.
    bool returnValue = false;            //Return value.

    if (socketHandle != IntPtr.Zero)
            //Attempt to create the destination address.
            remoteAddress = new sockaddr_in();
            remoteAddress.sin_family = SocketsConstants.AF_INET;
            remoteAddress.sin_port = htons((short)port);
            remoteAddress.sin_addr = inet_addr(ipAddress);
            remoteAddress.sin_zero = 0;

            //Check to ensure the address translated correctly.
            if (remoteAddress.sin_addr != 0)
                //Attempt to connect.
                resultCode = bind(socketHandle, ref remoteAddress,
                errorCode = Marshal.GetLastWin32Error();
                returnValue = (resultCode == 0);
            returnValue = false;
    return returnValue;


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version