Vous êtes sur la page 1sur 20

Date: 23-09-2013 M.E.

Electronics & Communication Engineering (VLSI &Embedded Systems Design) Semester: I Subject Name: Network Programming Subject Code: 2715202 Lab-Assignment:

1. Write a TCP Socket client server program for calc server, in which TCP client will send calculation request to the server. That request would contain float value as well. For Eg. add (a, b), where a=23.24, b=34.45, Server would able to interpret it and respond back with correct answer. Implement server for addition, division, multiplication and subtraction

Server: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <arpa/inet.h> #include <unistd.h> #define MYPORT 3490 #define BACKLOG 10

int main() { int sockfd, new_fd,status; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size,numbytes; char buf2[1000],buf[1000],buf1[1000],abuf[1000]; float c; sockfd = socket(AF_INET, SOCK_STREAM, 0); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(&(my_addr.sin_zero),0,8); bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr)); listen(sockfd, BACKLOG); while(1)
Jay Kothari Page 1 of 20

{ sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr*)&their_addr,&sin_size); printf("\nIP of client: %s is connecting. .",inet_ntoa(their_addr.sin_addr)); if (numbytes = recv(new_fd, buf, sizeof(buf),0) < 0) { perror("error in reciving 1st num . . \n"); }

float a= atof(buf); if (numbytes = recv(new_fd, buf1, sizeof(buf1),0) < 0) { perror("error in reciving 2nd num . . \n"); } float b = atof(buf1); if (numbytes = recv(new_fd, buf2, sizeof(buf2),0) < -1) { perror("error in reciving operation . . \n"); } printf("\nThe opration is: %s",buf2); if(strcmp("addition",buf2) == 0) { c = a + b; } else if (strcmp("subtraction",buf2) == 0) { c= a - b; } else if(strcmp("multiply",buf2) == 0) { c = a * b; } else if(strcmp("divison",buf2) == 0) { c = a/b; }

printf("\nThe Answer is: %f",c); sprintf(abuf,"%f",c); if(send(new_fd,abuf,sizeof(abuf),0) < 0) {


Jay Kothari Page 2 of 20

perror("\nerror in sending answer . . "); } memset(abuf,0,sizeof(abuf)); printf(" \n. . . program ends here . . . \n "); } close(new_fd); }

Client: #include<stdio.h> #include"calculator.h" int main() { float a=0,b=0,c=0; int i; printf("Enter 1st Num: "); scanf("%f",&a); printf("Enter 2nd Num: "); scanf("%f",&b); printf("Enter Your Choice Of Opration: \n"); printf("\n1. Addition: \n2. Substaction: \n3. Multiplication: \n4. Divison: \n"); printf("\nEnter your choice: "); scanf("%d",&i); printf("\n------------------------------------"); if(i==1) { c = addition(a,b); } else if(i==2) { c = sub(a,b); } else if(i==3) { c = mul(a,b); } else if(i==4) { c = divison(a,b); } else { printf("\nPlease enter correct number[1 to 4]");
Jay Kothari Page 3 of 20

} return 0; }

Calculator.h #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<sys/socket.h> #include<unistd.h> #include<netinet/in.h> #include<sys/types.h> #include<netdb.h> #define PORT 3490 int send_data(float i1, float i2,char buf3[2000]) { int sockfd,numbytes; char buf1[1000], buf2[1000],buf[2000],abuf[2000]; struct hostent* he; struct sockaddr_in their_addr; strcpy(buf,buf3);

sockfd = socket(AF_INET, SOCK_STREAM, 0); their_addr.sin_family = AF_INET; their_addr.sin_port = htons (3490); their_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); memset (&(their_addr.sin_zero),0,8);

connect(sockfd, (struct sockaddr*)&their_addr,sizeof(struct sockaddr)); printf("\n1st num is %f",i1); sprintf(buf1,"%f",i1); //printf("\nand its string is "); //puts(buf1); printf("\n2nd num is %f",i2); sprintf(buf2,"%f",i2); //printf("\nand its string is "); //puts(buf2); if(send(sockfd,buf1,sizeof(buf1),0)<0) { perror("error in sending 1st num. .");

Jay Kothari

Page 4 of 20

} if(send(sockfd,buf2,sizeof(buf2),0)<0) { perror("error in sending 2nd num . ."); } if(send(sockfd,buf,sizeof(buf),0)<0) { perror("error in sending opertion . ."); } printf("\nThe opration is: %s\n",buf); //puts(buf); if((numbytes = recv(sockfd,abuf,sizeof(abuf),0)) < -1); { // perror("error in recining answer. . "); } printf("\nThe Answer is :%s\n",abuf); //puts(abuf); memset(&abuf,0,sizeof(abuf));

return 0; } float addition(float z1,float z2) { send_data(z1,z2,"addition"); } float sub(float z1, float z2) { send_data(z1,z2,"subtraction"); }

float mul(float z1,float z2) { send_data(z1,z2,"multiply"); } float divison(float z1,float z2) { send_data(z1,z2,"divison"); }

Jay Kothari

Page 5 of 20

Output:

Jay Kothari

Page 6 of 20

Jay Kothari

Page 7 of 20

2. Write a UDP client server application in which client would get IP address of server. Client will enter command on client program, command will get executed on server, and server would respond back with specific filtered output. Server: #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/wait.h> #include<netinet/in.h> #include<arpa/inet.h> #include<unistd.h> #define BACKLOG 10 #define PORT 3494 int main(int argc,char* argv[]) { //Declaration of variables as per their respective data types and strucutre// int sockfd,sin_size,x,i=0,j=0,k=0,res=0,w=0; FILE *fp; struct sockaddr_in my_addr; struct sockaddr_in client_addr; char buffer1[500] ; char buffer2[500]; char str[50]; //Socket creation for UDP at server side// if((sockfd = socket(AF_INET, SOCK_DGRAM,0))==-1) { perror("\nERROR:SOCKET CANT BE CREATED!\n"); exit(1); } printf("\nYOUR SOCKET IS CREATED WITH ID %d\n",sockfd); //Insert respective data into struture for binding server's IP and Port address// my_addr.sin_family = AF_INET; my_addr.sin_port = htons(PORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); //Pad Zeros to unused part in strucutre// memset(&(my_addr.sin_zero),0,8); //NOW BIND THESE DATA// x = bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)); if(x == -1) { perror("\nERROR: BINDING FAILED!\n"); exit(1); } printf("\nBINDING SUCCESSFULL...\n"); while(1) {
Jay Kothari Page 8 of 20

sin_size = sizeof(struct sockaddr); memset(buffer1,'\0',sizeof(buffer1)); memset(buffer2,'\0',sizeof(buffer2)); //.............recvfrom() contain address of client's IP and port as an argument with its total length including data.......//

//

res=recvfrom(sockfd,buffer1,sizeof(buffer1),0,(structsockaddr*)&client_addr,&sin_size; buffer1[strlen(buffer1)]='\0'; printf("\nclient has sent command:"); puts(buffer1); printf("\nsize of 1st command is %d\n",strlen(buffer1));

//............data is stored in buffer... so for printing or copying its content ,one must insert a Null character at the end... //...............Printing IP of client and data sent by client to server's end.................//

if(sendto(sockfd,"Command received at server side",sizeof("command received at server side"),0,(struct sockaddr*)&client_addr,sin_size) == -1) { perror("ERROR:SENDING DATA TO CLIENT FAILED!"); exit(1); } res = recvfrom(sockfd,buffer2,sizeof(buffer2),0,(struct sockaddr*)&client_addr,&sin_size); buffer2[strlen(buffer2)]='\0'; printf("\nclient has sent string to be filtered is: %s\n",buffer2); printf("\nSERVER IS %s\n",inet_ntoa(client_addr.sin_addr)); NOW CONNECTED TO IP

//..............Now concate string received by server with ur" > <filename.txt>".. so that command can execute with given order...// strcat(buffer1," | grep "); strcat(buffer1,buffer2); strcat(buffer1," > serverinfo.txt"); printf("\nWE HAVE TO PERFORM %s\n",buffer1); //..............call system() for executing that command directly .................// system(buffer1); //..........Now open that perticular file on which we perfomed writing operation in read mode.....// fp = fopen("serverinfo.txt","r");

Jay Kothari

Page 9 of 20

//.........take a forever loop or any logic to read all contents of file and also store it to any buffer....// memset(buffer1,'\0',sizeof(buffer1)); i=0; while(1) { buffer1[i]=fgetc(fp); if(buffer1[i]==EOF) break; i++; } //...put Null character when total data has stored in buffer......// buffer1[i]='\0'; for(k=10,w=0;k<35;k++,w++) { str[w]=buffer1[k]; } str[w+1]='\0'; //......let user at server end confirm that ..all contents from file is copied into buffer by print it on screen....// printf("\ndata to be sent is:\n"); puts(buffer1); //........we have opened the file for reading purpose.. this task has completed now..so we have to close it right??..// fclose(fp); //.......as per defination..we have to pass all contents of file to client's end .. simple do this by sendto operation...// if(sendto(sockfd,str,sizeof(str),0,(struct sockaddr*)&client_addr,sin_size) == -1) { perror("ERROR:SENDING DATA TO CLIENT FAILED!"); exit(1); } } //.........All thing done.. now its time to close socket......// if((close(sockfd))==-1) { perror("\nERROR:SOCKET TERMINATION FAILED!\n"); exit(1); } return 0; }

Jay Kothari

Page 10 of 20

Client: #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<netdb.h> #include<sys/types.h> #include<netinet/in.h> #include<sys/socket.h> #include<unistd.h> #define PORT 3494 #define MAXSIZE 1024

int main(int argc,char* argv[]) fetching IP address and data.. { //...........Declaring Variables...................// int sockfd,numbytes,sin_size,x,n; char buffer[MAXSIZE]; struct sockaddr_in server_addr;

//We are using command line argument here for

//.......conforming total count of arguments......// if(argc != 4) { printf("\nUse as: <%s> <IP of Server> <Command> <'String to be Filtered' in double quatation>\n",argv[0]); exit(1); } //......Create socket at client end for UDP.....// if((sockfd = socket(AF_INET, SOCK_DGRAM,0)) == -1) { perror("\nERROR:SOCKET CAN'T BE CREATED!\n"); exit(1); } //......Insert respective values to struture for IP and PORT.....// server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.s_addr = inet_addr(argv[1]); memset(&(server_addr.sin_zero),0,8); sin_size = sizeof(struct sockaddr); //......put all arguments in sendto() for sending DATA to its respective server's end.....//
Jay Kothari Page 11 of 20

for(n=2;n<=3;n++) { x=sendto(sockfd,argv[n],strlen(argv[n]),0,(struct sockaddr*)&server_addr,sin_size);

if(numbytes = recvfrom(sockfd, buffer,sizeof(buffer),0,NULL,NULL) == -1) { perror("\nERROR:RECEIVE FUN. FAILED AT CLIENT's END\n"); exit(1); } //......Print total content all received buffer on screen......///. puts(buffer); } //......Terminate socket now......// if((close(sockfd))==-1) { perror("\nERROR:SOCKET TERMINATION FAILED!\n"); exit(1); } printf("\nSOCKET HAS BEEN TERMINATED SUCCESSFULLY AT CLIENT END...\n");

return 0; }

Jay Kothari

Page 12 of 20

Output:Client:-

Server:-

Jay Kothari

Page 13 of 20

3. Design High availability using socket. In this program, two PCs are continuously checking the status of each other, if it is up and in network or not. In case of any PC is not working or come out of network, other working PC will intimate the server about non availability of non working PC. Server: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define MYPORT 5050 #define BACKLOG 10 int main () { int sockfd, new_fd,status; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size,numbytes; char buf[101]; char buffer[100]; sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd<0) { printf("\nERROR WHILE SOCKET CREATION\n"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(&(my_addr.sin_zero), 0, 8); bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr)); listen(sockfd, BACKLOG); while(1) { sin_size = sizeof(struct sockaddr_in);
Jay Kothari Page 14 of 20

new_fd = accept(sockfd, (struct

sockaddr*)&their_addr,&sin_size);

if(fork()<0) { printf("\nerror in fork...\n"); } else if(fork()==0) { printf("\nIP OF CLIENT: %s\n",inet_ntoa(their_addr.sin_addr)); numbytes = read (new_fd, buf, sizeof(buf)); buf[numbytes] = '\0'; puts(buf); buffer[strlen(buffer)]='\0'; write(new_fd,"CONNECTION FAILED BY ANOTHER CLIENT", sizeof("CONNECTION FAILED BY ANOTHER CLIENT")); exit(status); } else { waitpid(-1,&status,0); } close(new_fd); } }

Client: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <unistd.h> #define PORT 8080 int main(int argc,char *argv[]) { if(argc<3) { printf("\nuse as: <%s> <IP to ping> <IP of Server>\n",argv[0]); exit(1); } int i=0,y,j=0; int sockfd,numbytes; char buffer[100],a[100],buf[100],store[100];
Jay Kothari Page 15 of 20

struct sockaddr_in their_addr; FILE *fd; memset(buffer,'\0',sizeof(buffer)); strcat(buffer,"ping -c 10 "); strcat(buffer,argv[1]); strcat(buffer," | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' > 1.txt"); // strcpy(buffer,"ping -c 10 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' > 1.txt"); system(buffer); fd=fopen("1.txt","r"); while(1) { a[i]=fgetc(fd); if(a[i]==EOF) break; i++; } fclose(fd); a[i]='\0'; puts(a); y=atoi(a); //printf("%d",y); if(y==10) {printf("\nnetwork is up\n"); j=1; } else {printf("\nnetwork is down\n"); j=2; }

if((sockfd = socket (AF_INET, SOCK_STREAM, 0))==-1) { printf("\nSOCKET CAN'T BE CREATED...\n"); exit(1); } printf("\nsocket is created...\n"); their_addr.sin_family = AF_INET; their_addr.sin_port = htons (PORT); their_addr.sin_addr.s_addr = inet_addr(argv[2]); memset (&(their_addr.sin_zero), 0, 8); if((connect (sockfd,(struct sockaddr*)&their_addr,sizeof (struct sockaddr)))<0) { printf("\nCONNECT FUNCTION FAILED...\n"); exit(1);
Jay Kothari Page 16 of 20

} printf("\nconnect function done..\n"); if(j==2) { memset(store,'\0',sizeof(store)); strcat(store,argv[1]); strcat(store," IS NOT REACHABLE..."); write(sockfd,store,sizeof(store)); } numbytes= read(sockfd, buf,sizeof(buf)); buf[numbytes] ='\0'; puts(buf); close (sockfd); }

Output:Client:-

Jay Kothari

Page 17 of 20

Server:-

4. Develop an application using socket programming to check the status of all live nodes in current sub-network.

Server:#include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/wait.h> #include<unistd.h> #include<sys/types.h> int main() { char node[20][20]; int p,q,r,s,status,y,t; FILE *fd; pid_t pid[20]; char buf[100],buffer[100]; printf("\nEnter Total number of nodes that you want to store\n"); scanf( "%d" , &p); fflush(stdin); for ( q = 0 ; q < p ; q++) { printf("\nEnter IP of node %d\t",q); scanf("%s",node[q]); fflush(stdin); }

while(1) { r=0; t=0; for(s=0;s<p;s++)


Jay Kothari Page 18 of 20

{ memset(buffer,'\0',sizeof(buffer)); strcat(buffer,"ping -c 2 "); strcat(buffer,node[s]); strcat(buffer," | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' > 1.txt"); system(buffer); fd=fopen("1.txt","r"); if(fd==NULL) printf("\nerror in file opening.\n"); q=0; while(1) { buf[q]=fgetc(fd); if(buf[q]==EOF) break; q++; } fclose(fd); buf[q]='\0'; y=atoi(buf); if(y==2) { printf("\nNODE %d- %s IS UP IN NETWORK...",s,node[s]); r=r+1; } else { printf("\nNODE %d- %s IS DOWN IN NETWORK...",s,node[s]); t=t+1; } } printf("\n........................................................\n\tTotal Number of Live nodes are: %d out of %d\n\tTotal Number of Dead nodes are: %d out of %d\n",r,p,t,p); } return 0; }

Jay Kothari

Page 19 of 20

Output:-

Jay Kothari

Page 20 of 20

Vous aimerez peut-être aussi