Commit efc835b7 authored by SHAILESH KUMAR's avatar SHAILESH KUMAR

server and client

parent 1c707917
No preview for this file type
......@@ -7,7 +7,67 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "toxml.h"
void sendXML(int sockfd,char *msg){
char cmd[4];
char key[257];
char value[256*1024+1];
char encodedXML[300000];
char msgtype[10];
char delim[2]="\n";
strcpy(key,"");
strcpy(value,"");
for(int i =0;i<3;i++){
cmd[i]=msg[i];
}
cmd[3]='\0';
if(!strcmp(cmd,"GET")){
strcpy(msgtype,"getreq");
strcat(key,msg+4);
key[strlen(key)-1]='\0';
//printf("%s",key);
}
else if(!strcmp(cmd,"PUT")){
strcpy(msgtype,"putreq");
char a[2];
for(int i=4;i<strlen(msg);i++){
if(msg[i]==' '){
strcat(value,msg+i+1);
break;
}
else{
a[0]=msg[i];
a[1]='\0';
strcat(key,a);
}
}
value[strlen(value)-1]='\0';
//printf("%s**%s",key,value);
}
else if(!strcmp(cmd,"DEL")){
strcpy(msgtype,"delreq");
strcat(key,msg+4);
key[strlen(key)-1]='\0';
//printf("%s",key);
}
else{
printf("error in command\n");
}
toXML(encodedXML,key,value,msgtype,"");
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
char buf[256];
read(sockfd,buf,256);
return;
}
void error(char *msg)
{
perror(msg);
......@@ -16,7 +76,7 @@ void error(char *msg)
int main(int argc, char *argv[])
{
int sockfd, portno, n;
int sockfd, portno, n,no_of_requests=0;
struct sockaddr_in serv_addr;
struct hostent *server;
......@@ -40,16 +100,23 @@ int main(int argc, char *argv[])
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
FILE *fp;
char* line=NULL;
size_t len=0;
fp=fopen("client_req.txt","r");
if(fp==NULL){
exit(1);
}
while((getline(&line,&len,fp))!=-1){
//write(sockfd,line,read);
no_of_requests++;
sendXML(sockfd,line);
}
while(1);
fclose(fp);
//exit(0);
return 0;
}
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "toxml.h"
void sendXML(int sockfd,char *msg){
char cmd[4];
char key[257];
char value[256*1024+1];
char encodedXML[300000];
char msgtype[10];
char delim[2]="\n";
strcpy(key,"");
strcpy(value,"");
for(int i =0;i<3;i++){
cmd[i]=msg[i];
}
cmd[3]='\0';
if(!strcmp(cmd,"GET")){
strcpy(msgtype,"getreq");
strcat(key,msg+4);
key[strlen(key)-1]='\0';
//printf("%s",key);
}
else if(!strcmp(cmd,"PUT")){
strcpy(msgtype,"putreq");
char a[2];
for(int i=4;i<strlen(msg);i++){
if(msg[i]==' '){
strcat(value,msg+i+1);
break;
}
else{
a[0]=msg[i];
a[1]='\0';
strcat(key,a);
}
}
value[strlen(value)-1]='\0';
//printf("%s**%s",key,value);
}
else if(!strcmp(cmd,"DEL")){
strcpy(msgtype,"delreq");
strcat(key,msg+4);
key[strlen(key)-1]='\0';
//printf("%s",key);
}
else{
printf("error in command\n");
}
toXML(encodedXML,key,value,msgtype,"");
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
char buf[256];
read(sockfd,buf,256);
return;
}
void error(char *msg)
{
perror(msg);
......@@ -15,7 +76,7 @@ void error(char *msg)
int main(int argc, char *argv[])
{
int sockfd, portno, n;
int sockfd, portno, n,no_of_requests=0;
struct sockaddr_in serv_addr;
struct hostent *server;
......@@ -39,16 +100,23 @@ int main(int argc, char *argv[])
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
FILE *fp;
char* line=NULL;
size_t len=0;
fp=fopen("client_req.txt","r");
if(fp==NULL){
exit(1);
}
while((getline(&line,&len,fp))!=-1){
//write(sockfd,line,read);
no_of_requests++;
sendXML(sockfd,line);
}
while(1);
fclose(fp);
//exit(0);
return 0;
}
1 hello world
0 hello
2 hello
0 hello
PUT hello world
GET hello
DEL hello
GET hello
PUT hello world
GET hello
DEL hello
GET hello
No preview for this file type
......@@ -11,6 +11,7 @@
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<string.h>
void dostuff(int); /* function prototype */
void error(char *msg)
{
......@@ -63,10 +64,12 @@ void dostuff (int sock)
int n;
char buffer[256];
while(1){
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
printf("Here is the message:\n %s\n",buffer);
n = write(sock,"I got your message\n",19);
if (n < 0) error("ERROR writing to socket");
}
}
......@@ -11,6 +11,7 @@
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<string.h>
void dostuff(int); /* function prototype */
void error(char *msg)
{
......@@ -63,10 +64,12 @@ void dostuff (int sock)
int n;
char buffer[256];
while(1){
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
printf("Here is the message:\n %s\n",buffer);
n = write(sock,"I got your message\n",19);
if (n < 0) error("ERROR writing to socket");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment