Call the Create member function after constructing a socket object to create the Windows socket and attach it.

BOOL Create(
   UINT nSocketPort = 0,
   int nSocketType = SOCK_STREAM,
   LPCTSTR lpszSocketAddress = NULL 


A well-known port to be used with the socket, or 0 if you want Windows Sockets to select a port.




A bitmask which specifies a combination of network events in which the application is interested.

  • FD_READ   Want to receive notification of readiness for reading.

  • FD_WRITE   Want to receive notification of readiness for writing.

  • FD_OOB   Want to receive notification of the arrival of out-of-band data.

  • FD_ACCEPT   Want to receive notification of incoming connections.

  • FD_CONNECT   Want to receive notification of completed connection.

  • FD_CLOSE   Want to receive notification of socket closure.


A pointer to a string containing the network address of the connected socket, 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.

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.

  • WSAEAFNOSUPPORT   The specified address family is not supported.

  • WSAEINPROGRESS   A blocking Windows Sockets operation is in progress.

  • WSAEMFILE   No more file descriptors are available.

  • WSAENOBUFS   No buffer space is available. The socket cannot be created.

  • WSAEPROTONOSUPPORT   The specified port is not supported.

  • WSAEPROTOTYPE   The specified port is the wrong type for this socket.

  • WSAESOCKTNOSUPPORT   The specified socket type is not supported in this address family.

Create calls Socket and if successful, it calls Bind to bind the socket to the specified address. The following socket types are supported:

  • SOCK_STREAM   Provides sequenced, reliable, full-duplex, connection-based byte streams. Uses the Transmission Control Protocol (TCP) for the Internet address family.

  • SOCK_DGRAM   Supports datagrams, which are connectionless, unreliable packets of a fixed (typically small) maximum length. Uses the User Datagram Protocol (UDP) for the Internet address family.


    The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.

Important noteImportant

Create is not thread-safe. If you are calling it in a multi-threaded environment where it could be invoked simultaneously by different threads, be sure to protect each call with a mutex or other synchronization lock.

For more information about stream and datagram sockets, see the articles Windows Sockets: Background and Windows Sockets: Ports and Socket Addresses and Windows Sockets 2 API.

Header: afxsock.h

Community Additions