ECHO APPLICATION USING UDP

Server program

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
void  dg_echo (int sockfd, struct sockaddr *pcliaddr, socklen_t clilen);
int  main (int argc, char **argv)
{
        int sockfd,port;
        struct sockaddr_in servaddr, cliaddr;
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        printf("Enter the port number : ");
        scanf("%d",&port);
        bzero (&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
       servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(port);
        bind (sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        dg_echo(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
 }
void  dg_echo (int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
 {
        int n;
        socklen_t len;
        char mesg[1000];
        len = clilen;
       while(strcmp(mesg,"bye")!=0)
        {
        n = recvfrom (sockfd, mesg, 1000, 0, pcliaddr, &len);
       printf("\nClient message :%s",mesg);
        sendto (sockfd, mesg, n, 0, pcliaddr, len);
        }
        exit(0);
}
Client program

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
void dg_cli(FILE *fp,int sockfd,struct sockaddr_in *psa,socklen_t servlen);

int  main (int argc, char **argv)
        {
        int sockfd,port;
        struct sockaddr_in servaddr;
        bzero (&servaddr, sizeof(servaddr));
        printf("Enter the port number  : ");
        scanf("%d",&port);
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(port);
        inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
       dg_cli (stdin,sockfd,(struct sockaddr_in *)&servaddr,sizeof(servaddr));
         exit(0);
        }
void dg_cli(FILE *fp,int sockfd,struct sockaddr_in *psa,socklen_t servlen)
        {
        int n;
        char sendline[1000], recvline[1000 + 1];
        while(strcmp(recvline,"bye")!=0)
        {
        printf("\nEnter the message   : ");
        while (fgets (sendline, 1000, fp) != NULL)
        {
          sendto(sockfd,sendline,100,0,psa,servlen);
          n = recvfrom(sockfd, recvline, 1000, 0, NULL, NULL);
          recvline[n] = 0;
          fputs (recvline, stdout);
        }
        exit(0);
        }
 }


OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 5echoser.c
[11ca013@mcalinux network]$ ./a.out
Enter the port number : 3326

Client message :hi

Client message :hello

Client message :how_are_you

Client message :i_am_fine

Client message :ok_bye

CLIENT

[11ca013@mcalinux network]$ cc 5echocli.c
[11ca013@mcalinux network]$ ./a.out 192.168.1.134
Enter the port number  : 3326
Enter the message  
hi
hi
hello
hello
how_are_you
how_are_you
i_am_fine
i_am_fine
ok_bye
ok_bye


0 Response to "ECHO APPLICATION USING UDP"

Post a Comment



Your IP Address is:

Browser: