Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
SHAILESH KUMAR
key-value-server
Commits
efc835b7
Commit
efc835b7
authored
Oct 04, 2019
by
SHAILESH KUMAR
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
server and client
parent
1c707917
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
179 additions
and
34 deletions
+179
-34
client
client
+0
-0
client.c
client.c
+80
-13
client.c~
client.c~
+81
-13
client_req.txt
client_req.txt
+4
-4
client_req.txt~
client_req.txt~
+4
-0
server
server
+0
-0
server.c
server.c
+5
-2
server.c~
server.c~
+5
-2
No files found.
client
View file @
efc835b7
No preview for this file type
client.c
View file @
efc835b7
...
...
@@ -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
;
}
client.c~
View file @
efc835b7
#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;
}
client_req.txt
View file @
efc835b7
1
hello world
0
hello
2
hello
0
hello
PUT
hello world
GET
hello
DEL
hello
GET
hello
client_req.txt~
0 → 100644
View file @
efc835b7
PUT hello world
GET hello
DEL hello
GET hello
server
View file @
efc835b7
No preview for this file type
server.c
View file @
efc835b7
...
...
@@ -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"
);
}
}
server.c~
View file @
efc835b7
...
...
@@ -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");
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment