You are on page 1of 3

1. Implementation of Echo / Talk commands.

Aim: To write the c program for implementing Socket Program Echo / Ping / Talk command. Algorithm: 1. Start the program 2. To establish a point to point communication between two processes. 3. Creation of sockets using system calls like Socket, bind, listen, connect, accept, establishes a communication link between the two processes. 4. The processes can communicate using Send and recv system calls. 5. The Echo command is used to display a line of text. 6. Ping is used to send ICMP echo request to network hosts. 7. Talk is a virtual communication program which copies lines from terminal to that of another user. 8. Stop the program.

PROGRAM: Client: #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> main() { struct sockaddr_in server,client; int sockfd,con,lis,acc,s,r,z; char m1[100]; char m2[100]; strcpy(m1,); strcpy(m2,); sockfd=socket(PF_INET,SOCK_STREAM,0); if(sockfd==-1) printf(Socket is not created); else printf(Socket is created); server.sin.family=PF_INET; server.sin.port=htons(3503);

server.sin_addr.s_addr=INADDR_ANY; memset(&(server.sin_zero),\0,8); client.sin_family=PF_INET; client.sin_port=htons(3503); client.sin_addr.s_addr=INADDR_ANY; memset(&(client.sin_zero),\0,8); client.sin_addr.s_addr=INADDR_ANY; memset(&(client.sin_zero),\0,8); con=connect(sockfd,(struct sockaddr *)& server,sizeof(struct sockaddr)); if(con==-1) printf( \n Connection Failed ); else printf( \n Connection Success ); r=recv(sockfd,&m1,sizeof(m1),0); printf( \n Received message is); printf(%s,m1); while((strcmp(m1,exit)!=0)&&(strcmp(m2,exit)!=0)) { printf(\n Enter Ur Message); scanf(%s,&m2); s=send(sockfd,&m2,sizeof(m2),0); r=recv(sockfd,&m1,sizeof(m1),0); printf(\n Received message is..); printf(%s,m1); } close(sockfd); }

Server: #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<string.h> main() { struct sockaddr_in server,client; int sockfd,bin,lis,acc,s,r,z,k; char m1[100]; char m2[100]; strcpy(m1,); strcpy(m2,); sockfd=socket(PF_INET,SOCK_STREAM,0); if(sockfd==-1)

printf(Socket is not created); else printf(Socket is created); server.sin.family=PF_INET; server.sin.port=htons(3503); server.sin_addr.s_addr=INADDR_ANY; memset(&(server.sin_zero),\0,8); client.sin_family=PF_INET; client.sin_port=htons(3503); client.sin_addr.s_addr=INADDR_ANY; memset(&(client.sin_zero),\0,8); bin=bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr)); if(bin==-1) printf( \n Bind not successful ); else printf( \n Bind Successful ); lis=listen(sockfd,5); if(lis==-1) printf(\n Not ready to listen ); else printf(\n Ready to listen ); k=sizeof(struct sockaddr_in); acc=accept(sockfd,(struct sockaddr *),&client,&z); if(acc==-1) printf( \n Not Accepted); else printf( \n Accepted); printf( \n Enter Ur Message); scanf(%s,&m1); s=send(acc,&m1,sizeof(m1),0); while((strcmp(m1,exit)!=0)&&(strcmp(m2,exit)!=0)) { r=recv(acc,&m2,sizeof(m2),0); printf(\n Received Message is ); printf(%s,m2); printf(\n Enter Ur message); scanf(%s,&m1); s=send(acc,&m1,sizeof(m1),0); } close(acc); }

You might also like