1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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: Unknown operation.\n");
return;
}
if(strlen(key)>256){
printf("ERROR: Key cannot exceed 256B");
return;
}
if(strlen(key)==0){
printf("ERROR: Key cannot be NULL");
return;
}
if(!strcmp(cmd,"PUT") && strlen(value)==0){
printf("ERROR: Value cannot be NULL");
return;
}
if(!strcmp(cmd,"PUT") && strlen(value)>(256*1024)){
printf("ERROR: Value cannot exceed 256KB");
return;
}
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);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n,no_of_requests=0;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = atoi("8080");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname("127.0.0.1");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
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);
sleep(1);
}
fclose(fp);
while(1);
//exit(0);
return 0;
}