Basic Socket communication APIs Explained

The Different Socket Communication APIs that need to be understood for setting up a socket communication link are 

  • Socket API
    1. The Socket API allows the creation of a socket and creates a file descriptor which is returned to the application requesting the same. The Socket API is provided below

domain – The domain parameter indicates the communication domain (for e.g.                                            AF_INET  -IPv4, AF_INET6  -IPv6, AF_UNIX for UNIX domain sockets etc)

Type – the type parameter indicates whether the socket being opened is a byte                                    stream (Stream sockets (SOCK_STREAM)) or message based                                                          (SOCK_DGRAM) or Raw socket stream (SOCK_RAW)

protocol field – The protocol field was included under the belief that a single protocol         family will support multiple families. however, it has not occurred that  multiple address families have been defined for a particular protocol family. Usually this parameter is set to 0 (zero). In special cases such as RAW sockets or netlink sockets – it might be set to a particular value  which we  will see later. (NOTE: AF stands for Address Family and PF       stands for Protocol Family – both are synonymous in with one another in most cases)

  • Bind API

The Bind API binds the socket obtained via socket API to an address or path (AF_UNIX). The API is shown below

sockfd –   The sockfd is the socket file descriptor obtained via socket API call

struct sockaddr *addr – The addr structure pointer indicates an address to which the socket needs to be bound. (Note : the socket structures for different address families are different- but the bind API takes in a generic sockaddr structure. The different address family socket structures are mapped to sockaddr during bind. We shall see this in the next article)

socklen_t addrlen – the addrlen parameter indicates the actual length of the structure element passed in the address parameter (addr). This parameter allows different structures to be mapped to sockaddr structure as is done in the second parameter of Bind API

  • listen API

The Listen API is used in Stream socket conections and is used to mark the socket as Passive. (Refer Active and Passive sockets here). The Listen API also has a backlog parameter which indicates the number of connections that can be pending for connection to a server. The Listen API is provided below

  • accept API

The accept API accepts an incoming connection (Stream sockets). The accept call is a blocking call and the invocation of accept API will block if there are no pending connection requests. The accept API is provided below

sockfd – the socket File descriptor

struct sockaddr *addr – the sockaddr structure pointer. This returns the address of the peer socket

socklen_t addrlen – the length of the structure member passed in parameter 2 of the API

  • connect API

The connect API is used to connect to a particular listening server socket. The server address and address length are passed as a parameter to the connect API.

sockfd – The socket file descriptor from the socket API call on the client side

struct sockaddr *addr – the socket address which is being connected to

socklen_t addrlen – length of the structure member passed in parameter 2 of the API

  • remove and unlink (in case of Unix Domain Sockets)

The Remove and unlink APIs are used to remove any residual previous socket connection. This is used particularly in the case of a UNIX domain socket wherein the previous file link for the socket might still be existing. The Remove and unlink APIs are provided below

int remove(const char *pathname);

remove calls unlink API for files and rmdir API for directories

int unlink(const char *pathname);

unlink deletes a name from the file system

The next article looks at creating a simple server and client socket connection and discusses the code in detail

A Socket Server/Client Example -TCP Stream Sockets

Comments

  1. Pingback: An introduction to Sockets | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *