Essay details:

  • Subject area(s): Engineering
  • Price: Free download
  • Published on: 7th September 2019
  • File format: Text
  • Number of pages: 2

Text preview of this essay:

This page is a preview - download the full version of this essay above.

CS 531



CS531 ' Fundamentals of System Programming




Lalith Krishna Chaitanya Gollapudi (G01025704)





1. Socket 3

1.1. Protocol 3

1.2. IP Address 3

1.3. Port Number 4

2. Client-server Communication 4

3. Socket Programming for TCP 4

4. Conclusion 7

5. Bibliography 7


The aim of the paper is to introduce sockets, its deployment pertaining to network programming. Sockets play an important role in client server applications, which are mainly web applications. This paper describes about network programming and functions involved with socket in C language. The client and server can communicate with each other by writing to or reading from these sockets which are present in between network layers. It gives basic knowledge about socket interface involved in creating network applications. One of the most basic network programming tasks likely to be faced as a C programmer is performing the socket functions/methods because C language has been preferred mostly for establishing client server communications using sockets.

1. Socket

Socket is an interface between application layer and transport layer which is used for communication between processes. Sockets are used for communication between a client program and a server program in a network. A socket is defined as endpoint in a connection. Sockets are Virtual parts of every network communication made by the two hosts on a link. For example, when we open any website in our browser, our machine creates a socket and then tries to make the connection with website which has already created a socket and waiting for connections. The same thing happens in skype or any communication networks wandering for socket connection. Socket can be created by set of programming requests or function calls which are specified in sockets application programming interface (API). The sockets API, Berkeley UNIX C interface for sockets is very famous. Inter process communication can happen in same machine or a different machine. Socket represents three things internally:

1. Protocol

2. IP address

3. Port number

1.1. Protocol

Inter Process communication uses well-defined formats which are protocols for exchanging various messages. Each message has an exact meaning intended to elicit a response from a range of possible responses pre-determined for that situation The specified behavior is typically independent of how it is to be implemented. Communications protocols should be agreed upon by the parties involved for standard communication. To reach agreement and set a standard between communication, protocols are set up and used standard communications.

1.2 IP Address

An Internet Protocol address ( IP ) is a numerical label(4 octets separated by a dot) given to every device (e.g., computer, printer) which is participating in a computer network that uses the Internet Protocol for inter process communication. An IP address has two important functions which are, and location addressing and host or network interface identification. Its role has been characterized as follows: "A name indicates what we seek. An address indicates where it is. A route indicates how to get there.' An IP address is a 32-bit number which is known as Internet Protocol Version 4 (IPv4), is still in use today. However, because of the growth of the Internet and the predicted depletion of available addresses, a new version of IP (IPv6) which uses 128 bits for the address, was developed very recently, in 1995.

1.3 Port Number

Inter process communications need a 'xed port for target communications. There are some ports which are reserved well known ports. Port usage:

0-1023 These ports can only be binded to by root

1024-5000 well known ports

5001-64K-1 ephemeral ports

2. Client ' Server Communication

Internet is connection of networks and networks are composed of machines which are either a client or a server. Server is a program that allows client to connect and offers some services but client is a program that is requesting some service. Servers are powerful machines or processes dedicated to managing disk drives (file servers), printers (print servers), or network traffic (network services) whereas clients are personal computers or workstations on which users run applications. Clients depend on servers for resources, such as devices, files and even processing power. When these programs are executed, a client and a server process are created simultaneously and these two processes communicate with each other by reading from and writing to sockets. Sockets are the programming interfaces which are found in between application layer and transport layer which provide TCP and UDP protocols for stream and datagram communication respectively. While creating a network application, developer's writes both the client and server side programs. The client/server application that is covered here is a proprietary client/server application. A single developer or a development team creates both the client and server side programs, and the developer has complete control over what goes in the code. But because the code written is not to implement a public-domain protocol, developers other than owners the code will not be able to develop code that interoperates with the application. When developing a proprietary application it is necessary that the developer must be careful not to use one of the well-known port numbers defined in the RFCs.

3. Socket Programming for TCP

Computer network programming involves writing computer programs that communicate with processes across a computer network. We need to write two different programs, Client program which initiates the communication and the server program which waits or listens for the communication from client. Both endpoints of the communication are called network sockets. Network programming is also called as socket programming. Network Programing for TCP can be implemented using C language provides following network layer functions.


The syntax of the function is defined as follows:

#include <sys/socket.h>

int sockid = socket(domain, type, protocol);

This creates a socket with specific the type of communication protocol, family and type. Domain specifies the protocol family (AF_INET for the IPv4 protocols), type is a constant described the type of socket (SOCK_STREAM for stream sockets and SOCK_DGRAM for datagram sockets). The function returns a file descriptor(which is a non-negative integer) or -1 on error.


The function is defined as follows:

#include <sys/socket.h>

int status = connect(sock, &name, namelen);

The connect() function is used by a TCP client to establish a connection with a TCP server. sock is the socket descriptor returned by the socket function. The function returns 0 on establishing a connection (i.e., successful TCP three-way handshake), -1 otherwise. The client does not have to call bind() before calling this function. The kernel will choose an ephemeral port and the source IP if required.


#include <sys/socket.h>

int status = bind(sockid, &addrport, size);

The bind() function associates a socket id with an address to which other processes can connect. In internet protocol the address is [ipNumber, portNumber]. The address is the combination of an IPv4 which is 32-bit or IPv6which is 64-bit address with a 16 bit TCP port number. sockid is the socket descriptor, addrport is a pointer to a protocol-specific address and size is the size of the address structure. The function returns 0 on success and -1 on an error. This use of the generic socket address sockaddr structure which is defined in c library, requires that any calls to these functions must cast the pointer to the protocol-specific address structure.

The generic:

struct sockaddr {

u_short sa_family;

char sa_data[14];


sa_family: specifies which address family is being used

determines how the remaining 14 bytes are used For unix sockets (only works between processes on the same machine)

The Internet-specific:

struct sockaddr_in {

short sin_family;

u_short sin_port;

struct in_addr sin_addr;

char sin_zero[8];};

When using internet sockets, the second parameter of bind (of type sockaddr in *) must be cast to (sockaddr *).

Byte Ordering

Byte Ordering is very important in network world, because Big Endian format is used by network world. But some of the machines which try to connect to internet are using little Endian, So we need to convert the ordering appropriately for the machines.

Host Byte Order to Network Byte Order(Big Endian to little Endian):



Network Byte Order to Host Byte Order(Little Endian to Big Endian):



IP Number Translation

IP address is an 32 bit number which is divided into 4 octets each of 8 bit separated by dot(.). The IP addess should be converted from asci to Binary for the machine to understand.

Ascii to Binary: inet_aton()

Binary to Ascii dotted: inet_ntoa()


#include <sys/socket.h>

int status = listen(sock, queuelen);

The listen() function connects an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. Parameters of this function, sock which is the socket descriptor and queuelen is the maximum number of connections the kernel should queue for this socket. The queuelen argument provides a hint to the system of the number of outstanding connection requests that it should enqueue on behalf of the process. The system will reject all other connection requests when its queue is full. The queuelen value must be chosen based how many requests a server can handle. The function will return 0 on success and -1 on error.


#include <sys/socket.h>

int s = accept(sock, &name, &namelen);

The accept() function waits for an incoming request and when it is received it creates a socket for it. sock is a file descriptor which is created when it is connected to the client, after connect() function. The &name and &namelen arguments are used to return the address structure of the client. The new socket descriptor has the same characteristics of the original socket. The original socket passed to accept() is not associated with the connection, but instead remains available to receive additional connect requests. The kernel creates a new or unique connected socket for each client connection that is accepted. If we don't want to know about the client's identity then we can set the name and namelen arguments to NULL. If not, before calling the accept function, the name parameter has to be set to a buffer large enough to hold the address and set the interger pointed by namelen to the size of the buffer.


#include <sys/socket.h>

int count = send(sock, &buf, len, flags);

Since a socket is represented as a file descriptor when it is created and it provides uniqueness, we can communicate between socket as long as we are connected by send and recv functions. However, if we want to specify options we need another set of functions. buf and len have the same meaning as they have with write. The additional argument flags is used to specify how we want the data to be transmitted. Flag can either be a 0 as default or MSG OOB which is Out-of-band high priority communication. The function returns the number of bytes on success and -1 on error.


#include <sys/socket.h>

int count = recv(sock, &buf,  len, flags);

The recv() function is similar to read(), but this function allows to specify some options to control how the data should be received. Flags can be 'MSG OOB' which is out-of-bound message, 'I MSG PEEK' which is look at message without removing and 0 as default. The function returns the length of the message in bytes and 0 when there are no messages, -1 on error.


#include <unistd.h>

int status = close(s);

Socket is closed and terminated when we call close() function. The function returns 0 if it succeeds and -1 on an error.

4. Conclusion

This paper describes the details about sockets, IP address, protocol, port, client- server communication and socket programming in TCP. Network programming makes use of socket for interprocess communication between hosts where sockets are the endpoints of the interprocess communication. The paper describes basic  functionality of socket programming and functions used in socket programing(C language) for TCP. C language has been preferred, than any other language for establishing connections between clients and servers using sockets.

5. Bibliography

[1].  Advanced Programming in the UNIX Environment by W. Richard Stevens and Stephen A. Rago

[2].  Socket Programming in C Language by R.Rajivarnan  ( Cyber Security Researcher )

[3].  Network programming in Java using Socket by Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, pp

...(download the rest of the essay above)

About this essay:

This essay was submitted to us by a student in order to help you with your studies.

If you use part of this page in your own work, you need to provide a citation, as follows:

Essay Sauce, . Available from:< > [Accessed 25.05.20].