Network Programming Notes

Hi Aspirants,

We'll learn about network programming today. As a core concept of Networks, it goes deep and requires a good amount of time to cover. We'll try to limit it to the scope of specialist officer's exams. If anyone interested in deep learning, raise it in comments, I'll share the sources.

==>> This article is a part of PK Series

What is Inter Process Communication (IPC)?

IPC is a mechanism that allows exchange of data between processes, by using a set of programming interfaces. The IPC mechanism can be classified into Pipes (FIFO) and Shared memory (Semaphores). In pipes, data flow is unidirectional. A pipe is generally created by invoking a pipe system call which generates a pair of file descriptors. We'll learn about shared memory concepts and semaphores in Operating Systems.

Network Programming

 What is a file descriptor?

In simple words, when you open a file, OS creates an entry to represent that file and stores information about that file (somewhere in kernel). These entries are integers and are called file descriptors. So if your process opens 10 files, your process table will 10 entries for file descriptors. Similarly, when you open a network socket, it is represented by an integer which is called socket descriptor. 

What is a socket?

The socket is a BSD method for accomplishing IPC. It allows one process to speak to another and works very similar to files i.e read/write on a socket is similar to that of files. A socket is created using the socket( ) system call. 

Types of Sockets

Two most common types are:
  • SOCK_STREAM: Stream sockets, which provide reliable two way connection oriented communication streams. It uses TCP.
  • SOCK_DGRAM: Datagram sockets, which provide connectionless, unreliable service, used for packet by packet transfer of information. It uses User Datagram Protocol (UDP)

System Calls using Sockets

1. socket( ) - returns socket descriptor, -1 on error.
#include <sys/types.h>
#include <sys/socket.h>
int socket (int domain, int type, int protocol);

domain should be set to AF_INET (uses ip address)
protocol- set to zero typically.

2. bind( ) - associates the socket with an address, -1 on error.
#include <sys/types.h>
#include <sys/socket.h>
int socket (int sockfd, struct sockaddr *my_addr, int addrlen);

sockfd- socket file descriptor returned by socket( ).
my_addr- pointer to a structure that contains information about local IP address and port number.
addrlen- set to sizeof (struct sockaddr)

3. connect( ) - connect to a remote socket, -1 on error
#include <sys/types.h>
#include <sys/socket.h>
int socket (int sockfd, struct sockaddr *serv_addr, int addrlen);

serv_addr- pointer to a structure that contains the destination IP address and port number

4. listen( ) - wait for incoming connections, -1 on error
int listen (int sockfd, int backlog);

backlog- set max no of requests that will be queued before requests are denied.

5. accept( )- returns a new socket file descriptor for every single connection, -1 on error
#include <sys/socket.h>
int accept (int sockfd, void *addr, int *addrlen);

addr- pointer to a local structure sockaddr_in, here information about incoming connection will be stored
addrlen- should be set to sizeof(struct sockaddr_in) before accept( ) is called.

Other systems calls along with rest of the topic will be covered in next article.

System Calls using Sockets

6. send( ) & recv( ) - used for communicating over stream sockets or connected datagram sockets, -1 on error.
int send (int sockfd, char *buffer, int buf_len, int flags);
int recv (int sockfd, char *buffer, int buf_len, int flags)

buffer- buffer to read the information into
buf_len - length of data in bytes
flags - typically set to 0

send( ) returns the number of bytes actually sent out and recv ()returns the number of bytes actually read into the buffer. 

7. sendto( ) & recvfrom( )- transmit and receive data packets over unconnected datagram sockets, -1 on error.
int sendto (int sockfd, char *buffer, int buf_len, int flags, const struct sockaddr *to, int tolen);
int recvfrom (int sockfd, char *buffer, int buf_len, int flags, struct sockaddr *from, int *fromlen);

to- address of the target
tolen- size of the structure pointed to by to
from- socket address structure from which data is received
fromlen- size of from in bytes 

8. close( ) & shutdown( ) - used to close the connection on socket descriptor 
close (sockfd);  //prevents anymore reads/writes to the socket
int shutdown (int sockfd, int how); 

how =0 - prohibits further receives
how=1 - prohibits further sends
how = 2 - prohibits further sends/receives

9. getpeername( ) - this will tell you who is at the other end of a connection stream socket
#include <sys/socket.h>
int getpeername (int sockfd, struct sockaddr *addr, int *addrlen) 

addr - pointer to a structure that holds information about other side of connection
addrlen - pointer to an int that is initialized to sizeof(struct sockaddr)

10. gethostname( ) - returns name of the computer on which your program is running
#include <unistd.h>   //header for standard symbolic constants and types
int gethostname (char *hostname, size_t size);

hostname- pointer to an array of chars which contains the hostname
size - length of hostname array in bytes

11. gethostbyname( ) - returns the IP address of a host given its name, invokes the DNS
#include <netdb.h>   //header for network database operations
struct hostent *gethostbyname (const char *name);   //returns a pointer to struct hostend

Client Server communication for connection oriented protocols

Network Programming

Client Server communication for connectionless protocols

Network Programming


1. SOCK_STREAM sockets are used by which processes?
a) UDP
b) TCP
d) none of the above 

2. SOCK_DGRAM sockets are used by which processes?
a) UDP
b) TCP
c)  SCTP
d) none of the above

 Quote of the day 

The secret of getting ahead is getting started. -Mark Twain

                                                                                                                                             Deepak A 
Join 40,000+ readers and get free notes in your email


Post a Comment

Thanks for commenting. It's very difficult to answer every query here, it's better to post your query on