Online Labs

Transfer Control Protocol(TCP)



The activity involved in a tcp server are……….

                                                1.Create a socket.

                                                2.Bind it to the operating system.

                                                3.Listen over it.

                                                4.Accept connections.

                                                5.Read/Write processes.

                                                6.Close the socket.









/*Program to demonstrate the creation and usage of tcp socket.

* This module acts as the tcp server which listens to a socket and accepts connection.

* The server initially reads a message from the client and then writes a message to it.

* This module should be compiled using the command. ‘c++ tcpserver.cpp’ and execute     











int main()


            //variables to store the socket id.

 int serversocket,clientsocket;

            //variables to store the network host addresses.

 sockaddr_in serveraddr,clientaddr;

            //variable to store address length.

 socklen_t len;

 char message[50];


                        //creating a socket.



                        //steps to include the host address






                        //binding the socket to the operating system.






                        //listening over the socket.



 printf(“\nWaiting for client connectivity.\n”);


                        //accepting the connection.



 printf(“\nClient connectivity received.\n”);


 printf(“\nReading message from the client.\n”);


                        //reading activity.



 printf(“\nThe client has send:\t%s\n”,message);


 printf(“\nSending message to the client.\n”);


                        //writing activity.

 write(clientsocket,”YOUR MESSAGE RECEIVED.”,sizeof(“YOUR MESSAGE RECEIVED.”));













The different processes involved in a udp client process are……..

                                    1.Create a datagram socket.

                                    2.Receive/Send message to the server.

                                    3.Close the socket.




/* Program to demonstrate the creation and usage of datagram sockets.

* This module acts as a udp client which sends and receives messages from a udp server.

* This module should be compiled into different folder using the command ‘c++ 

   udpclient.cpp -o b’ and execute using ‘./b’

* For execution the ‘udpserver’ module should be executed first then this module.











int main()


                                                //variable to store the socket_id.

 int clientsocket;

                                                //variable to store the address.

 sockaddr_in serveraddr;                                  

                                                //variableto store the address length.

 socklen_t len;

                                    //variable to store the network byte order address.

 hostent *server;

 char message[50];

                                                //socket creation.


                                                //steps involved in the server address creation.











 printf(“\nSending message for server connection\n”);

                                                //sending message.

 sendto(clientsocket,”HI I AM CLIENT…”,sizeof(“HI I AM CLIENT….”),0,(sockaddr*)&serveraddr,sizeof(serveraddr));

 printf(“\nReceiving message from server.\n”);

                                                //receiving messages.



 printf(“\nMessage received:\t%s\n”,message);










Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: