/* Networking Network Client Server C Program */
/* Posting more than one Program Of Each, Any one may run. Should Try all . Some Debugging may require. Program had run using RED HAT LINUX. Dont know whether runs on any other OS or not*/
/* Works on RED HAT LINUX */
Client Program No1.
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<sys/time.h>
#define REMOTE_SERVER_PORT 1500
int main(int argc,char *argv[])
{ char msg[10];
int sd,rc,i,client_len;
struct sockaddr_in client_address,remote_server_address;
if(argc<3) { printf("\n Number of arguments is less than <3 \n");exit(1);}
remote_server_address.sin_family=AF_INET;
remote_server_address.sin_addr.s_addr=inet_addr(argv[1]);
remote_server_address.sin_port=htons(REMOTE_SERVER_PORT);
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) { printf("\n Cannot open socket \n ");exit(1); }
client_address.sin_family=AF_INET;
client_address.sin_addr.s_addr=htonl(INADDR_ANY);
client_address.sin_port=htons(0);
rc=bind(sd,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Cannot bind port \n"); exit(1);}
rc=sendto(sd,argv[2],strlen(argv[2]),0,(struct sockaddr*)&remote_server_address, sizeof(remote_server_address));
if(rc<0) { printf("\n Cannot send data \n "); close(sd);exit(1);}
while(1)
{ memset(msg,0x0,10);
client_len=sizeof(remote_server_address);
rc=recvfrom(sd,msg,2,0,(struct sockaddr*)&remote_server_address,&client_len);
if(rc<0) { printf("\n Cannto Receive data \n ");
continue;
}
printf("\n\n Result Received From Server % d \n ",msg[0]);
}
close(sd);
return 1;
}
Client Program No 2
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<sys/time.h>
#define REMOTE_SERVER_PORT 1500
int main(int argc,char *argv[])
{ char msg[100];
int sd,rc,i,client_len;
struct sockaddr_in client_address,remote_server_address;
if(argc<3) { printf("\n Number of arguments is less than <3 \n");exit(1);}
remote_server_address.sin_family=AF_INET;
remote_server_address.sin_addr.s_addr=inet_addr(argv[1]);
remote_server_address.sin_port=htons(REMOTE_SERVER_PORT);
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) { printf("\n Cannot open socket \n ");exit(1); }
client_address.sin_family=AF_INET;
client_address.sin_addr.s_addr=htonl(INADDR_ANY);
client_address.sin_port=htons(0);
rc=bind(sd,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Cannot bind port \n"); exit(1);}
rc=sendto(sd,argv[2],strlen(argv[2]),0,(struct sockaddr*)&remote_server_address, sizeof(remote_server_address));
if(rc<0) { printf("\n Cannot send data \n "); close(sd);exit(1);}
while(1)
{ memset(msg,0x0,100);
client_len=sizeof(remote_server_address);
rc=recvfrom(sd,msg,2,0,(struct sockaddr*)&remote_server_address,&client_len);
if(rc<0) { printf("\n Cannto Receive data \n ");
continue;
}
//printf("\n Rc = %d",rc);
if(rc==2)
{ printf("\n\n Result Received From Server % d \n ",msg[0]); break;}
}
close(sd);
return 1;
}
Server Program No. 1
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define LOCAL_SERVER_PORT 1500
int main(int argc, char *argv[])
{ char msg[10],x[2];
int sd,rc,i,client_len,a,b,c;
struct sockaddr_in client_address,server_address;
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{ printf("\n Can not open Socket \n ");
exit(1);
}
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(LOCAL_SERVER_PORT);
rc=bind(sd,(struct sockaddr *) &server_address,sizeof(server_address));
if(rc<0)
{ printf("\n Can not bind port number \n ");
exit(1);
}
while(1)
{ memset(msg,0x0,10);
client_len=sizeof(client_address);
rc=recvfrom(sd,msg,10,0,(struct sockaddr *)&client_address,&client_len);
if(rc<0) { printf("\n Cannot receive data \n "); continue;}
printf("\n Received from Client %d %d %d \n",msg[0],msg[1],msg[2]);
a=msg[0];b=msg[2];
a=a-48;b=b-48;c=a+b;x[0]=c;
printf("\n In the Server the Result of Computation is %d \n\n",x[0]);
break;
}
rc=sendto(sd,x,2,0,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Can not send data \n ");
close(sd); exit(1);}
return 1;
}
Server Program No. 2
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define LOCAL_SERVER_PORT 1500
int main(int argc, char *argv[])
{ char msg[10],x[2];
int sd,rc,i,client_len,a,b,c;
struct sockaddr_in client_address,server_address;
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{ printf("\n Can not open Socket \n ");
exit(1);
}
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(LOCAL_SERVER_PORT);
rc=bind(sd,(struct sockaddr *) &server_address,sizeof(server_address));
if(rc<0)
{ printf("\n Can not bind port number \n ");
exit(1);
}
while(1)
{ memset(msg,0x0,10);
client_len=sizeof(client_address);
rc=recvfrom(sd,msg,10,0,(struct sockaddr *)&client_address,&client_len);
if(rc<0) { printf("\n Cannot receive data \n "); continue;}
printf("\n Received from Client %d %d %d \n",msg[0],msg[1],msg[2]);
a=msg[0];b=msg[2];
a=a-48;b=b-48;c=a+b;x[0]=c;
if(msg[1]=='+') c=a+b;
else if(msg[1]=='-') c=a-b;
else if(msg[1]=='*') c=a*b;
else if(msg[1]=='/') c=a/b;
else if(msg[1]=='%') c=a%b;
x[0]=c;
printf("\n In the Server the Result of Computation is %d \n\n",x[0]);
break;
}
rc=sendto(sd,x,2,0,(struct sockaddr*)&client_address,sizeof(client_address));
if(rc<0) { printf("\n Can not send data \n ");
close(sd); exit(1);}
return 1;
}
No comments:
Post a Comment