Online Labs

User Datagram Protocol(UDP)

Server

 

Algorithm

 

* The different process involved in an udp server process are…….

                                                1.Create a datagram socket.

                                                2.Bind the socket to the operating system.

                                                3.Receive/Sending processes.

                                                4.Close the socket.

 

Program

 

/* Program to demonstrate the creation and use of datagram socket.

 

* This module act as udp server which waits for client connection

 

* This server initially receives message from the client and then sends back a reply.

 

* This module should be comipled 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()

{

                                    //variable to store the socket_id.

 int serversocket;

                                    //variable to store the network addresses.

 sockaddr_in serveraddr,clientaddr;

                       

                                    //variable to store the address length.

 socklen_t len;

 char message[50];

 

                                    //socket creation.

 serversocket=socket(AF_INET,SOCK_DGRAM,0);

 

                                    //steps involved in defining the serveraddress.

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

 serveraddr.sin_family=AF_INET;

 serveraddr.sin_port=htons(5015);

 serveraddr.sin_addr.s_addr=INADDR_ANY;

 

                                    //binding the socket to the operating system.

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

 

 printf(“\nWaiting for the client connection\n”);

 

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

 len=sizeof(clientaddr);

 

                                    //receiving message from the client.

 recvfrom(serversocket,message,sizeof(message),0,(sockaddr*)&clientaddr,&len);

 

 printf(“\nConnection received from client.\n”);

 

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

 

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

 

                                    //sending message to the client.

 sendto(serversocket,”YOUR MESSAGE RECEIVED.”,sizeof(“YOUR MESSAGE RECEIVED.”),0,(sockaddr*)&clientaddr,sizeof(clientaddr));

 

 close(serversocket);

}

  

Output

111

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

 

29

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

Blog at WordPress.com.

%d bloggers like this: