Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

The following procedure shows how to create a socket server application.

Procedures

To create a socket server application
  1. Perform name resolution to obtain a list of addresses to serve on.

  2. Create a list of serving sockets, one for each address, using the socket function.

  3. Name the sockets with the bind (Windows Sockets)function using the addresses returned from the getaddrinfofunction.

    When you open a socket with socket, the socket has no name assigned to it. However, a descriptor for the socket is allocated in an address family namespace. To assign a name to the server socket, call bind. The sockets of the server application need to be named with the bindfunction. However, it is unnecessary to give a client socket a name by using bind.

  4. For TCP stream sockets, listen for incoming client connections with the listenfunction.

  5. Wait for incoming data (connection-less) or connections (connection-oriented).

    For TCP, accept a client connection with the accept (Windows Sockets)function.

    The acceptfunction creates a new socket. The original socket opened by the server continues to listen and can be used to accept more connections until closed. Server applications must close the listening socket, in addition to any sockets created, by accepting a client connection.

  6. For connection-oriented applications, send and receive data with a client using the sendand recvfunctions.

    For connection-less applications, send and receive data with a client using the sendtoand recvfromfunctions.

    For applications that require compatibility with both connection-oriented and connectionless sockets, always use sendtoand recvfrom.

    The send and recvfunctions can also be used with connectionless sockets. A socket must be connected before calling sendor recv, however it is not recommended to use connecton a connectionless socket under Ipv6. Due to the multi-homed aspect of IPv6, data will be received on a public address but will likely be sent from an anonymous address. Incoming data on an address other than the one passed to connect will be quietly discarded.

    Successfully completing a call to sendor sendtodoes not confirm that data was successfully delivered.

  7. For connection-oriented sockets, shut down the socket with the shutdownfunction.

  8. When data exchange between the server and client ends, close the socket with the closesocketfunction. An application should call shutdownbefore calling closesocket.

    An application should always have a matching call to closesocketfor each successful call to socketto return any socket resources to the system. For TCP stream sockets, when a socket connection ends the server must close the socket created by accept. If the server does not close the socket originally returned by the first call to socket, that socket continues to listen for clients. When the server disconnects or is out of service, it should call closesocketto close any listening sockets.

See Also