Introduction

    Sockets are used with TCP/IP networking (UDP also, but less). TCP/IP is connection-oriented protocol and so in higher levels in TCP/IP protocol stack we must provide a standard for identifying a single TCP connection. Sockets API (application program interface) is going to implement this task.

    By standard definition, socket is a pair: IP address + port number, where: IP address is actually a unique computer identifier and port number is a unique application identifier. So a pair of sockets defines a single connection between two applications
that may run on different computers. No port can be used by two different sockets on
the same computer.

    The standard situation is that one application (a client) initiates a connection, while another (a server) "listens" on a certain port and when it receives a request from client, sockets API creates a connection between them (two-way communication).

    A connection is created in the following way (Diagram 1):

    1. Server listens continuously on a certain port for incoming connection request.
       (No two servers can listen on the same port).
    2. When client initiates a connection with its socket (sends a connection
       request to server), it specifies an IP address and a port number it connects to.
       After the connection is established, the server allocates a specific socket
       for that connection, and connects this socket to the client socket.
      3. Actually, server can listen many times on the same listening socket
       and so may serve many clients.  ( server specific sockets will
       be distinct, of course).
 
 

Diagram 1 - Establishing a connection

    In this article we will focus our discussion on WinSock which is implementation of sockets (sockets API) for Windows. We will describe in detail this API and its specific functions. Of course, other operating systems (UNIX, VAX etc.) have also socket implementations, but they're all similar, so we think that it is enough to describe in detail only one specific API. WinSock is based on UNIX/BSD implementation of sockets (Berkeley sockets).