Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


Call this member function to associate a local address with the socket.

BOOL Bind(
   UINT nSocketPort,
   LPCTSTR lpszSocketAddress = NULL 
BOOL Bind (
   const SOCKADDR* lpSockAddr,
   int nSockAddrLen 



The port identifying the socket application.


The network address, a dotted number such as "". Passing the NULL string for this parameter indicates the CAsyncSocket instance should listen for client activity on all network interfaces.


A pointer to a SOCKADDR structure that contains the address to assign to this socket.


The length of the address in lpSockAddr in bytes.

Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

  • WSANOTINITIALISED   A successful AfxSocketInit must occur before using this API.

  • WSAENETDOWN   The Windows Sockets implementation detected that the network subsystem failed.

  • WSAEADDRINUSE    The specified address is already in use. (See the SO_REUSEADDR socket option under SetSockOpt.)

  • WSAEFAULT   The nSockAddrLen argument is too small (less than the size of a SOCKADDR structure).

  • WSAEINPROGRESS   A blocking Windows Sockets call is in progress.

  • WSAEAFNOSUPPORT   The specified address family is not supported by this port.

  • WSAEINVAL   The socket is already bound to an address.

  • WSAENOBUFS   Not enough buffers available, too many connections.

  • WSAENOTSOCK   The descriptor is not a socket.

This routine is used on an unconnected datagram or stream socket, before subsequent Connect or Listen calls. Before it can accept connection requests, a listening server socket must select a port number and make it known to Windows Sockets by calling Bind. Bind establishes the local association (host address/port number) of the socket by assigning a local name to an unnamed socket.

Community Additions

© 2015 Microsoft