Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-store
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Samarth Joshi
key-value-store
Commits
37accc82
Commit
37accc82
authored
Oct 25, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
messages added
parent
dbb1720f
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
242 additions
and
124 deletions
+242
-124
KVClient.c
KVClient.c
+74
-44
KVMessageFormat.h
KVMessageFormat.h
+5
-0
KVServer.c
KVServer.c
+60
-79
Makefile
Makefile
+1
-1
a.out
a.out
+0
-0
client
client
+0
-0
server
server
+0
-0
temp.c
temp.c
+102
-0
No files found.
KVClient.c
View file @
37accc82
...
...
@@ -9,66 +9,96 @@ send GET , PUT , and DELETE requests to the server process. It will have a main
client library interfaces to do the actual operations.
*/
#include <netdb.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
struct
message
*
request
(
char
status
,
char
*
key
,
char
*
value
)
{
if
(
!
status
||
(
key
==
NULL
&&
value
==
NULL
)
)
{
printf
(
"Invalid parameters in request()"
);
return
NULL
;
}
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
requestMessage
->
status
=
status
;
memcpy
(
requestMessage
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
requestMessage
->
key
)
<
256
)
{
requestMessage
->
key
[
strlen
(
requestMessage
->
key
)]
=
'\0'
;
}
if
(
value
!=
NULL
)
{
memcpy
(
requestMessage
->
value
,
value
,
256
);
//copied the complete value
}
if
(
strlen
(
requestMessage
->
value
)
<
256
)
//to pad with \0
{
requestMessage
->
value
[
strlen
(
requestMessage
->
value
)]
=
'\0'
;
}
printf
(
"[Message Generated at Client]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
return
requestMessage
;
}
struct
message
*
requestMessage
;
void
func
(
int
sockfd
)
{
char
buff
[
MAX
];
int
n
;
for
(;;)
{
bzero
(
buff
,
sizeof
(
buff
));
printf
(
"Enter the string : "
);
n
=
0
;
while
((
buff
[
n
++
]
=
getchar
())
!=
'\n'
)
;
write
(
sockfd
,
buff
,
sizeof
(
buff
));
bzero
(
buff
,
sizeof
(
buff
));
read
(
sockfd
,
buff
,
sizeof
(
buff
));
printf
(
"From Server : %s"
,
buff
);
if
((
strncmp
(
buff
,
"exit"
,
4
))
==
0
)
{
printf
(
"Client Exit...
\n
"
);
break
;
}
}
char
message
[
256
]
=
"helloworld"
;
requestMessage
=
request
(
'g'
,
message
,
NULL
);
send
(
sockfd
,
requestMessage
,
sizeof
(
struct
message
),
0
);
}
int
main
()
int
main
(
int
argc
,
char
const
*
argv
[]
)
{
int
sock
fd
,
connf
d
;
struct
sockaddr_in
serv
addr
,
cli
;
// socket create and varification
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
==
-
1
)
{
printf
(
"
socket creation failed...
\n
"
);
exit
(
0
)
;
int
sock
=
0
,
valrea
d
;
struct
sockaddr_in
serv
_addr
;
char
*
hello
=
"Hello from client"
;
char
buffer
[
1024
]
=
{
0
};
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
<
0
)
{
printf
(
"
\n
Socket creation error
\n
"
);
return
-
1
;
}
else
printf
(
"Socket successfully created..
\n
"
);
bzero
(
&
servaddr
,
sizeof
(
servaddr
));
// assign IP, PORT
servaddr
.
sin_family
=
AF_INET
;
servaddr
.
sin_addr
.
s_addr
=
inet_addr
(
"127.0.0.1"
);
servaddr
.
sin_port
=
htons
(
PORT
);
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_port
=
htons
(
PORT
);
// Convert IPv4 and IPv6 addresses from text to binary form
if
(
inet_pton
(
AF_INET
,
"127.0.0.1"
,
&
serv_addr
.
sin_addr
)
<=
0
)
{
printf
(
"
\n
Invalid address/ Address not supported
\n
"
);
return
-
1
;
}
// connect the client socket to server socket
if
(
connect
(
sockfd
,
(
SA
*
)
&
servaddr
,
sizeof
(
servaddr
))
!=
0
)
{
printf
(
"
connection with the server failed...
\n
"
);
exit
(
0
)
;
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
printf
(
"
\n
Connection Failed
\n
"
);
return
-
1
;
}
else
printf
(
"connected to the server..
\n
"
);
char
key
[
256
]
=
"abc"
;
char
value
[
256
]
=
"cde"
;
struct
message
*
requestMessage
=
request
(
'g'
,
key
,
value
);
struct
message
*
replyMessage
=
malloc
(
sizeof
(
struct
message
));
send
(
sock
,
requestMessage
,
sizeof
(
struct
message
)
,
0
);
printf
(
"Hello message sent
\n
"
);
// function for chat
func
(
sockfd
);
valread
=
read
(
sock
,
replyMessage
,
sizeof
(
struct
message
));
printf
(
"[Message Received from client]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]"
,
replyMessage
->
status
,
replyMessage
->
key
,
replyMessage
->
value
);
// close the socket
close
(
sockfd
);
return
0
;
}
\ No newline at end of file
KVMessageFormat.h
View file @
37accc82
...
...
@@ -18,3 +18,8 @@ Error: 400 (with the appropriate error message)
Reasons for error could be GET key not found, DEL key not found etc.
*/
struct
message
{
char
status
;
char
key
[
256
];
char
value
[
256
];
};
KVServer.c
View file @
37accc82
...
...
@@ -63,8 +63,10 @@ error. (Assume each character to be 1 byte in size)
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
...
...
@@ -72,95 +74,74 @@ error. (Assume each character to be 1 byte in size)
// Function designed for chat between client and server.
void
func
(
int
sockfd
)
{
char
buff
[
MAX
];
printf
(
"receive functions:"
);
char
buff
[
sizeof
(
struct
message
)];
int
n
;
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
// infinite loop for chat
for
(;;)
{
bzero
(
buff
,
MAX
);
// read the message from client and copy it in buffer
read
(
sockfd
,
buff
,
sizeof
(
buff
));
read
(
sockfd
,
buff
,
sizeof
(
struct
message
));
// print buffer which contains the client contents
printf
(
"From client: %s
\t
To client : "
,
buff
);
bzero
(
buff
,
MAX
);
n
=
0
;
// copy server message in the buffer
while
((
buff
[
n
++
]
=
getchar
())
!=
'\n'
)
;
// and send that buffer to client
write
(
sockfd
,
buff
,
sizeof
(
buff
));
// if msg contains "Exit" then server exit and chat ended.
if
(
strncmp
(
"exit"
,
buff
,
4
)
==
0
)
{
printf
(
"Server Exit...
\n
"
);
break
;
}
}
printf
(
"From client: %c
\t
To client : "
,
buff
[
0
]);
}
// Driver function
int
main
()
int
main
(
int
argc
,
char
const
*
argv
[]
)
{
int
sockfd
,
connfd
,
len
;
struct
sockaddr_in
servaddr
,
cli
;
// socket create and verification
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
==
-
1
)
{
printf
(
"socket creation failed...
\n
"
);
exit
(
0
);
}
else
printf
(
"Socket successfully created..
\n
"
);
bzero
(
&
servaddr
,
sizeof
(
servaddr
));
// assign IP, PORT
servaddr
.
sin_family
=
AF_INET
;
servaddr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
servaddr
.
sin_port
=
htons
(
PORT
);
// Binding newly created socket to given IP and verification
if
((
bind
(
sockfd
,
(
SA
*
)
&
servaddr
,
sizeof
(
servaddr
)))
!=
0
)
{
printf
(
"socket bind failed...
\n
"
);
exit
(
0
);
int
server_fd
,
new_socket
,
valread
;
struct
sockaddr_in
address
;
int
opt
=
1
;
int
addrlen
=
sizeof
(
address
);
char
buffer
[
1024
]
=
{
0
};
char
*
hello
=
"Hello from server"
;
// Creating socket file descriptor
if
((
server_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
0
)
{
perror
(
"socket failed"
);
exit
(
EXIT_FAILURE
);
}
else
printf
(
"Socket successfully binded..
\n
"
);
// Now server is ready to listen and verification
if
((
listen
(
sockfd
,
5
))
!=
0
)
{
printf
(
"Listen failed...
\n
"
);
exit
(
0
);
}
else
printf
(
"Server listening..
\n
"
);
len
=
sizeof
(
cli
);
// Accept the data packet from client and verification
while
(
1
)
{
//
connfd
=
accept
(
sockfd
,
(
SA
*
)
&
cli
,
&
len
);
if
(
connfd
<
0
)
// Forcefully attaching socket to the port 8080
if
(
setsockopt
(
server_fd
,
SOL_SOCKET
,
SO_REUSEADDR
|
SO_REUSEPORT
,
&
opt
,
sizeof
(
opt
)))
{
printf
(
"server acccept failed...
\n
"
);
exit
(
0
);
perror
(
"setsockopt
"
);
exit
(
EXIT_FAILURE
);
}
else
printf
(
"server acccept the client...
\n
"
);
address
.
sin_family
=
AF_INET
;
address
.
sin_addr
.
s_addr
=
INADDR_ANY
;
address
.
sin_port
=
htons
(
PORT
);
// Forcefully attaching socket to the port 8080
if
(
bind
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
sizeof
(
address
))
<
0
)
{
perror
(
"bind failed"
);
exit
(
EXIT_FAILURE
);
}
connfd
=
accept
(
sockfd
,
(
SA
*
)
&
cli
,
&
len
);
if
(
connfd
<
0
)
{
p
rintf
(
"server acccept failed...
\
n
"
);
exit
(
0
);
if
(
listen
(
server_fd
,
3
)
<
0
)
{
p
error
(
"liste
n"
);
exit
(
EXIT_FAILURE
);
}
else
printf
(
"server acccept the client...
\n
"
);
// Function for chatting between client and server
func
(
connfd
);
// After chatting close the socket
close
(
sockfd
);
if
((
new_socket
=
accept
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
(
socklen_t
*
)
&
addrlen
))
<
0
)
{
perror
(
"accept"
);
exit
(
EXIT_FAILURE
);
}
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
valread
=
read
(
new_socket
,
requestMessage
,
sizeof
(
struct
message
));
printf
(
"[Message Received from client]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
struct
message
*
replyMessage
=
malloc
(
sizeof
(
struct
message
));
replyMessage
->
status
=
'0'
;
char
buff
[
256
]
=
"success"
;
memcpy
(
replyMessage
->
key
,
buff
,
256
);
replyMessage
->
value
[
0
]
=
'\0'
;
send
(
new_socket
,
replyMessage
,
sizeof
(
struct
message
)
,
0
);
printf
(
"
\n
[Message sent to client]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]"
,
replyMessage
->
status
,
replyMessage
->
key
,
replyMessage
->
value
);
return
0
;
}
\ No newline at end of file
Makefile
View file @
37accc82
all
:
KVClient.c KVServer.c
all
:
KVClient.c KVServer.c
KVMessageFormat.h
gcc KVServer.c
-o
server
gcc KVClient.c
-o
client
\ No newline at end of file
a.out
0 → 100755
View file @
37accc82
File added
client
View file @
37accc82
No preview for this file type
server
View file @
37accc82
No preview for this file type
temp.c
0 → 100644
View file @
37accc82
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "KVMessageFormat.h"
struct
message
*
get
(
char
m
,
char
*
key
)
{
if
(
!
m
||
key
==
NULL
)
{
printf
(
"Invalid parameters in get()"
);
return
NULL
;
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
{
request
->
key
[
strlen
(
request
->
key
)]
=
'\0'
;
}
request
->
value
[
0
]
=
'\0'
;
return
request
;
}
struct
message
*
put
(
char
m
,
char
*
key
,
char
*
value
)
{
if
(
!
m
||
(
key
==
NULL
&&
value
==
NULL
))
{
printf
(
"Invalid parameters in put()"
);
return
NULL
;
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
{
request
->
key
[
strlen
(
request
->
key
)]
=
'\0'
;
}
memcpy
(
request
->
value
,
value
,
256
);
//copied the complete value
if
(
strlen
(
request
->
key
)
<
256
)
//to pad with \0
{
request
->
key
[
strlen
(
request
->
value
)]
=
'\0'
;
}
return
request
;
}
struct
message
*
del
(
char
m
,
char
*
key
){
if
(
!
m
||
key
==
NULL
)
{
printf
(
"Invalid parameters in del()"
);
return
NULL
;
}
struct
message
*
request
=
malloc
(
sizeof
(
struct
message
));
request
->
status
=
m
;
memcpy
(
request
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
request
->
key
)
<
256
)
{
request
->
key
[
strlen
(
request
->
key
)]
=
'\0'
;
}
request
->
value
[
0
]
=
'\0'
;
return
request
;
}
struct
message
*
request
(
char
status
,
char
*
key
,
char
*
value
)
{
if
(
!
status
||
(
key
==
NULL
&&
value
==
NULL
)
)
{
printf
(
"Invalid parameters in request()"
);
return
NULL
;
}
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
requestMessage
->
status
=
status
;
memcpy
(
requestMessage
->
key
,
key
,
256
);
//copied the complete key
if
(
strlen
(
requestMessage
->
key
)
<
256
)
{
requestMessage
->
key
[
strlen
(
requestMessage
->
key
)]
=
'\0'
;
}
if
(
value
!=
NULL
)
{
memcpy
(
requestMessage
->
value
,
value
,
256
);
//copied the complete value
}
if
(
strlen
(
requestMessage
->
value
)
<
256
)
//to pad with \0
{
requestMessage
->
value
[
strlen
(
requestMessage
->
value
)]
=
'\0'
;
}
printf
(
"[Message Generated at Client]
\n
[[Status:%c]
\n
[Key:%s]
\n
[Value:%s]]"
,
requestMessage
->
status
,
requestMessage
->
key
,
requestMessage
->
value
);
return
requestMessage
;
}
void
main
()
{
char
message
[
256
];
for
(
int
i
=
0
;
i
<
30
;
i
++
)
{
message
[
i
]
=
'a'
;
}
message
[
sizeof
(
message
)]
=
'\0'
;
request
(
'g'
,
message
,
message
);
//creating a message
}
\ No newline at end of file
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