Online Labs

Transfer Control Protocol(TCP)

Server

Algorithm

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

 

 

/*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     

    ‘./a.out’.*/

 

                        //inculsion.

#include<iostream>

#include<stdio.h>

 

#include<sys/types.h>

#include<netinet/in.h>

#include<netdb.h>

 

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.

 serversocket=socket(AF_INET,SOCK_STREAM,0);

 

                        //steps to include the host address

 bzero((char*)&serveraddr,sizeof(serveraddr));

 serveraddr.sin_family=AF_INET;

 serveraddr.sin_port=htons(5030);

 serveraddr.sin_addr.s_addr=INADDR_ANY;

 

                        //binding the socket to the operating system.

 bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));

 

 bzero((char*)&clientaddr,sizeof(clientaddr));

 len=sizeof(clientaddr);

 

                        //listening over the socket.

 listen(serversocket,5);

 

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

 

                        //accepting the connection.

 clientsocket=accept(serversocket,(sockaddr*)&clientaddr,&len);

 

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

 

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

 

                        //reading activity.

 read(clientsocket,message,sizeof(message));

 

 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.”));

 

 close(clientsocket);

 close(serversocket);

}

 

 

Output

110

Client

 

Algorithm

 

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

 

/* 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.

 

 

                                                //inculsion.

#include<iostream>

#include<stdio.h>

 

#include<sys/types.h>

#include<netinet/in.h>

#include<netdb.h>

 

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.

 clientsocket=socket(AF_INET,SOCK_DGRAM,0);

                                                //steps involved in the server address creation.

 bzero((char*)&serveraddr,sizeof(serveraddr));

 len=sizeof(serveraddr);

 serveraddr.sin_family=AF_INET;

 serveraddr.sin_port=htons(5015);

 server=gethostbyname(“127.0.0.1”);

 bcopy((char*)server->h_addr,(char*)&serveraddr.sin_addr.s_addr,sizeof(server->h_addr));

 

 printf(“\nPRESS ENTER TO START THE CONNECTION PROCESS.\n”);

 fgets(message,2,stdin);

 

 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.

 recvfrom(clientsocket,message,sizeof(message),0,(sockaddr*)&serveraddr,&len);

 

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

 

 close(clientsocket);

}

 

Output

28

 

 

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: