All about programming in GNU/LINUX

SOCKET/Networking programming in GNU/LINUX

Network Programming in GNU/LINUX:Importance of socket ports and Difference in coding of TCP and UDP socket applications

TCP and UDP are both transport layer protocols which involves process to process communication .As it is very important to identify the recipient device Locally or over Internet since

there will more than one computer in the network , it is equally important to identify right process in the receivers computer to which information has to be send since there will hundreds of processes running.

We’ll consider a practical scenario , Let us assume that users on system1 and system2 are communicating over INTERNET using 2 different programs , a Chat application and a VOIP based Application like SKYPE. The data sent from the Chat Application process from the system1 has to reach the chat application process running the system2 , not to any other process and it should follow he same in case of the VOIP application .The concept of software or logical ports help us identify the right process to receive the information across the network once the device is identified using its Network layer address(IP address) or data link layer address(MAC address).

 The concept of a network capable application or a process running on logical port ensures that the communication takes place between intended processes at the transport layer .

 TCP is connection oriented with many more checking mechanisms used to ensure message delivery and completeness , UDP has minimal checking, does not maintain a connection or ensure completeness or delivery of message.

TCP(Transmission Control Protocol). TCP is a connection-oriented protocol, a connection can be made from client to server, and from then on any data can be sent along that connection.

    • Reliable – when you send a message along a TCP socket, you know it will get there unless the connection fails completely. If it gets lost along the way, the server will re-request the lost part. This means complete integrity, things don’t get corrupted.

    • Ordered – if you send two messages along a connection, one after the other, you know the first message will get there first. You don’t have to worry about data arriving in the wrong order.

    • Heavyweight – when the low level parts of the TCP “stream” arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together.

  1. UDP(User Datagram Protocol). A simpler message-based connectionless protocol. With UDP you send messages(packets) across the network in chunks.

    • Unreliable – When you send a message, you don’t know if it’ll get there, it could get lost on the way.

    • Not ordered – If you send two messages out, you don’t know what order they’ll arrive in.

    • Lightweight – No ordering of messages, no tracking connections, etc. It’s just fire and forget! This means it’s a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets.


While writing TCP based socket applications the connection has to be first established b/w the two process in the sender and reciever devices before any exchange of information could take place .the sequence of system calls involved in creating a TCP client/server program are as follows .


The connect call from the client establishes the connection b/w client and the server only after which the information exchange takes place .The following figure shows the sequence of calls involved in creating a TCP based Client/server application.

The following figure shows shows the sequence of calls involved in creating UDP based client/server application



Network Programming in GNU/LINUX- socket() system call


The elementary system call in an any SOCKET/NETWORK program(server/client) in GNU/LINUX written using C is socket().The call to socket() creates a socket ,that is a communication end point.The prototype for the socket call is as follows

#include <sys/socket.h>

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

The domain(first argument) can be one of the following values .It specifies the address family(AF)

     Name                   Purpose                 
       AF_UNIX, AF_LOCAL      Local communication              
       AF_INET                IPv4 Internet protocols        
       AF_INET6               IPv6 Internet protocols
       AF_IPX                 IPX - Novell protocols
       AF_NETLINK             Kernel user interface device    
       AF_X25                 ITU-T X.25 / ISO-8208 protocol 
       AF_AX25                Amateur radio AX.25 protocol
       AF_ATMPVC              Access to raw ATM PVCs
       AF_APPLETALK           Appletalk                      
       AF_PACKET              Low level packet interface 

  The socket has the indicated type(second argument), which specifies the communication semantics. Currently defined types are:





Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.



Supports datagrams (connectionless, unreliable messages of a fixed maximum length).



Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read system call.



Provides raw network protocol access.



Provides a reliable datagram layer that does not guarantee ordering.



Obsolete and should not be used in new programs

CAUTION:All combinations of domain(first argument) and type(second argument) are not valid.

The third argument protocol  specifies  a  particular  protocol  to  be used with the    socket.  Normally only a single protocol exists to support a particular  socket  type within a given protocol family, in which case protocol can  be specified as 0.

       On  success,  a  file  descriptor  for  the new socket is returned.  On
       error, -1 is returned, and errno is set appropriately.

       EACCES Permission to create a socket of the specified type and/or  protocol is denied.

        The  implementation  does not support the specified address family.


       Unknown protocol, or protocol family not available.


       Invalid flags in type.


       Process file table overflow.


       The system limit on the total number  of  open  files  has  been  reached.

              Insufficient  memory is available.  The socket cannot be created
              until sufficient resources are freed.

              The protocol type or the specified  protocol  is  not  supported  within this domain.

Network Programming in GNU/LINUX:Introduction on building a server application in GNU/LINUX using C socket API’s

/*This is an Introduction on building a server application in GNU/LINUX using C socket API’s*/
/*Run this program and issue “netstat -ant | grep 7004” in another terminal, you can see the server listening */

/*accept system call doesnot return till either it encounters an error or gets a connection from a client(such system calls are called as blocking system call */
int main()
    int sd,len;
    struct sockaddr_in servaddr,cliaddr;//socket structure
    sd=socket(AF_INET,SOCK_STREAM,0);/*elementary call in any client server application to create an socket*/

    bzero(&servaddr,sizeof(servaddr));//to initialize all elements of the structure to zero
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);//To assian a arbitrary IP to the server
    servaddr.sin_port=htons(7004);//port on which the server listens
    bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr));/*assign the IP and port information to the socket */
    listen(sd,5);/*Listen for incoming coonections */
    accept(sd,(struct sockaddr *)&cliaddr,&len);/*accept the incoming connections*/
    return 0;