Share via


Creating a Socket Server Application (Windows Embedded CE 6.0)

1/6/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 getaddrinfo function.

    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 bind function. 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 listen function.

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

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

    The accept function 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 send and recv functions.

    For connection-less applications, send and receive data with a client using the sendto and recvfrom functions.

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

    The send and recv functions can also be used with connectionless sockets. A socket must be connected before calling send or recv, however it is not recommended to use connect on 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 send or sendto does not confirm that data was successfully delivered.

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

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

    An application should always have a matching call to closesocket for each successful call to socket to 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 closesocket to close any listening sockets.

    For Windows Embedded CE, for an example of a socket server application, see Socket Server.

See Also

Tasks

Creating a Socket Client Application

Concepts

Stream Socket Application
Socket Client