Showing posts with label IMPLEMENTATION OF PING COMMAND USING SOCKETS. Show all posts
Showing posts with label IMPLEMENTATION OF PING COMMAND USING SOCKETS. Show all posts

PING COMMAND USING SOCKETS

Server program

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
//#define port 1567
int main()
{
        int sock,con,byte_rec,flag=0,port;
        char send_data[1024],rec_data[1024];
        struct sockaddr_in ser,cli;
        int sin_size,i;
        if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
        {
                perror("Socket\n");
                exit(1);
        }

        ser.sin_family=AF_INET;
        printf("Enter the port number   :");
        scanf("%d",&port);
        ser.sin_port=htons(port);
        ser.sin_addr.s_addr=INADDR_ANY;
        bzero(&(ser.sin_zero),8);
        if(bind(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1)
        {
                perror("can't bind\n");
                exit(1);
        }
        if(listen(sock,3)==-1)
        {
                perror("listen\n");
                exit(1);
        }
        printf("Waiting \n");
        sin_size=sizeof(struct sockaddr_in);
        con=accept(sock,(struct sockaddr*)&cli,&sin_size);
        printf("Connected\n");

        while(1)
        {
                byte_rec=recv(con,rec_data,1024,0);
                rec_data[byte_rec]='\0';

                printf("\n\n\t DATA RECEIVED : %s" ,rec_data);
                if(strcmp(rec_data,"end") == 0)
                  break;
                send(con,rec_data,strlen(rec_data),0);
        }
        close(con);
        close(sock);
        return 0;
}

Client program

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<errno.h>
#include<sys/time.h>
int main()
{
        int sock,byte_rec,i=0,port;
        char send_data[1024],rec_data[1024];
        long sec1,sec2,msec1,msec2;
        struct hostent *host;
        struct sockaddr_in ser,cli;
        struct timeval tv;
        struct timezone tz;
        host=gethostbyname("192.168.1.134");
        if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
        {
                perror("Socket\n");
                exit(1);
        }
        printf("socket created\n");
        ser.sin_family=AF_INET;
        printf("Enter the port number   :");
        scanf("%d",&port);
        ser.sin_port=htons(port);
        ser.sin_addr= *((struct in_addr *)host->h_addr);
        bzero(&(ser.sin_zero),8);
        if(connect(sock,(struct sockaddr*)&ser,sizeof(struct sockaddr))==-1)
        {
                perror("connect\n");
                exit(1);
        }
        while(1)
        {
                printf("Enter the data to be sent : ");
                scanf("%s",send_data);
                i++;
                gettimeofday(&tv,&tz);
                sec1=tv.tv_sec;
                msec1=tv.tv_usec;
                send(sock,send_data,strlen(send_data),0);
                if(strcmp(send_data,"end")==0)
                  break;
                byte_rec=recv(sock,rec_data,1024,0);
                gettimeofday(&tv,&tz);
                sec2=tv.tv_sec;
                msec2=tv.tv_usec;
                rec_data[byte_rec]='\0';
                printf("Data received : %s\n",rec_data);
                sec1=sec2-sec1;
                msec1=msec2-msec1;
                msec1=msec1+(sec1*1000000);
                printf("64 bytes received from 192.168.1.134 : ICMP seq no - %d:
 time - %ld microseconds\n",i+1,msec1);
        }
        close(sock);                                                        
        return 0;
}




OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 7pingser.c
[11ca013@mcalinux network]$ ./a.out
Enter the port number   :7624
Waiting
Connected

         DATA RECEIVED : ping
         DATA RECEIVED : hi
         DATA RECEIVED : how
         DATA RECEIVED : welcome
         DATA RECEIVED : end

CLIENT

[11ca013@mcalinux network]$ cc 7pingclie.c
[11ca013@mcalinux network]$ ./a.out
socket created
Enter the port number   :7624
Enter the data to be sent : ping
Data received : ping
64 bytes received from 192.168.1.134 : ICMP seq no - 2: time - 86 microseconds
Enter the data to be sent : hi
Data received : hi
64 bytes received from 192.168.1.134 : ICMP seq no - 3: time - 83 microseconds
Enter the data to be sent : how
Data received : how
64 bytes received from 192.168.1.134 : ICMP seq no - 4: time - 66 microseconds
Enter the data to be sent : welcome
Data received : welcome
64 bytes received from 192.168.1.134 : ICMP seq no - 5: time - 69 microseconds

Enter the data to be sent : end



Your IP Address is:

Browser: