UDP ECHO CLIENT SERVER

AIM:
           To write a program for UDP echo client server.

ALGORITHM:
SERVER:

    STEP 1: Start
    STEP 2: Declare the variables for the socket
    STEP 3: Specify the family, protocol, IP address and port number
    STEP 4: Create a socket using socket() function
    STEP 5: Bind the IP address and Port number
    STEP 6: Listen and accept the client’s request for the connection
    STEP 7: Read and Display the client’s message
    STEP 8: Stop
CLIENT:
    STEP 1: Start
    STEP 2: Declare the variables for the socket
    STEP 3:  Specify the family, protocol, IP address and port number
    STEP 4: Create a socket using socket() function
    STEP 5: Call the connect() function
    STEP 6: Read the input message
    STEP 7: Send the input message to the server
    STEP 8: Display the server’s echo
    STEP 9: Close the socket
    STEP 10: Stop

SOURCE CODE:
SERVER:

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
#define MAXLINE 1024
int main(int argc,char **argv)
{
int sockfd;
int n;
socklen_t len;
char msg[1024];
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(5035);
printf("\n\n Binded");
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("\n\n Listening...");
for(;;)
{
      printf("\n ");
     len=sizeof(cliaddr);
     n=recvfrom(sockfd,msg,MAXLINE,0,(struct sockaddr*)&cliaddr,&len);
     printf("\n Client's Message : %s\n",msg);
     if(n<6)
        perror("send error");
     sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len);
}
return 0;
}

CLIENT:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdio.h>
#define MAXLINE 1024
int main(int argc,char* argv[])
{
int sockfd;
int n;
socklen_t len;
char sendline[1024],recvline[1024];
struct sockaddr_in servaddr;
strcpy(sendline,"");
printf("\n Enter the message : ");
scanf("%s",sendline);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(5035);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
len=sizeof(servaddr);
sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len);
n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
recvline[n]=0;
printf("\n Server's Echo : %s\n\n",recvline);
return 0;
}

OUTPUT:
SERVER:
















CLIENT:


















RESULT:
    Thus the program for UDP echo client server was executed and the output was verified.
Previous
Next Post »

1 comments:

Write comments
Shiva Kumar
AUTHOR
April 15, 2015 at 1:03 AM delete

Really Helpful
Thanks a lot

PS: Kindly change the image for server side, i think got confused with TCP and UDP ;)

Reply
avatar

If you still didn't find what you're looking for, you can search this website below: