Showing posts with label IMPLEMENTATION OF ECHO APPLICATION USING TCP. Show all posts
Showing posts with label IMPLEMENTATION OF ECHO APPLICATION USING TCP. Show all posts

ECHO APPLICATION USING TCP

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>
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;
        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\n");
        while(1)
        {
                byte_rec=recv(con,rec_data,1024,0);
                rec_data[byte_rec]='\0';
                printf("received data : %s\n\n",rec_data);
                send(con,rec_data,strlen(rec_data),0);
                if(strcmp(rec_data,"end")==0)
                        break;
        }
        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>

int main()
{
        int sock,byte_rec,port;
        char send_data[1024],rec_data[1024];
        struct hostent *host;
        struct sockaddr_in ser,cli;
        host=gethostbyname("127.0.0.1");
        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("\nsend:");
                scanf("%s",send_data);
                send(sock,send_data,strlen(send_data),0);
                byte_rec=recv(sock,rec_data,1024,0);
                rec_data[byte_rec]='\0';
                printf("Rec data : %s\n",rec_data);

                if(strcmp(rec_data,"end")==0)
                        break;
        }
        close(sock);
        return 0;
}






















OUTPUT

SERVER

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

received data : hi

received data : hello

received data : how_are_you

received data : i_am_fine

received data : end

[11ca013@mcalinux network]$

CLIENT

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

send:hi
Rec data : hi

send:hello
Rec data : hello

send:how_are_you
Rec data : how_are_you

send:i_am_fine
Rec data : i_am_fine

send:end
Rec data : end

[11ca013@mcalinux network]$



Your IP Address is:

Browser: