Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pa4
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nilesh Jagdish
pa4
Commits
60e210f4
Commit
60e210f4
authored
Nov 13, 2020
by
Nilesh Jagdish
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uploaded graph
parent
4a50fd11
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
160 deletions
+11
-160
client.c
client.c
+0
-148
client.cpp
client.cpp
+11
-12
graph.png
graph.png
+0
-0
No files found.
client.c
deleted
100644 → 0
View file @
4a50fd11
#include <stdio.h>
#include "client.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
int
connectToServer
(
char
*
addr
,
char
*
portNo
)
{
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
struct
addrinfo
hints
,
*
result
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
// getaddrinfo return getaddr objects by searching using the provided hints, name, service
int
s
=
getaddrinfo
(
addr
,
portNo
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
return
-
1
;
}
connect
(
sockfd
,
result
->
ai_addr
,
result
->
ai_addrlen
);
sendToServer
(
sockfd
);
return
0
;
}
void
sendToServer
(
int
sockfd
)
{
printf
(
"Enter the index no to choose an option...
\n
"
);
printf
(
"------OPTIONS------
\n
1. GET
\n
2. PUT
\n
3. DELETE
\n
4. EXIT
\n
"
);
int
choice
;
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
int
number_of_requests
=
0
;
struct
timespec
start
,
end
;
clock_gettime
(
CLOCK_MONOTONIC_RAW
,
&
start
);
while
(
1
)
{
printf
(
"Enter choice: "
);
scanf
(
"%d"
,
&
choice
);
char
key
[
100
],
value
[
100
];
switch
(
choice
)
{
case
1
:
printf
(
"Key: "
);
scanf
(
"%s"
,
key
);
GET
(
key
,
buffer
);
break
;
case
2
:
printf
(
"Key: "
);
scanf
(
"%s"
,
key
);
printf
(
"Value: "
);
scanf
(
"%s"
,
value
);
PUT
(
key
,
value
,
buffer
);
break
;
case
3
:
printf
(
"Key: "
);
scanf
(
"%s"
,
key
);
DELETE
(
key
,
buffer
);
break
;
case
4
:
printf
(
"Requested for connection termination
\n
"
);
terminateConnection
(
buffer
);
// return NULL;
break
;
}
// printf("buffer : %s\n", buffer);
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
1000
];
int
len
=
read
(
sockfd
,
recvBuffer
,
999
);
recvBuffer
[
len
]
=
'\0'
;
printf
(
"%s
\n
"
,
recvBuffer
);
if
(
choice
==
4
)
{
clock_gettime
(
CLOCK_MONOTONIC_RAW
,
&
end
);
uint64_t
delta_us
=
(
end
.
tv_sec
-
start
.
tv_sec
)
*
1000000
+
(
end
.
tv_nsec
-
start
.
tv_nsec
)
/
1000
;
printf
(
"Throughput : %f
\n
"
,
(
float
)
number_of_requests
/
(
float
)
delta_us
);
float
tput
;
FILE
*
ptr
=
fopen
(
"client.txt"
,
"r"
);
if
(
!
ptr
)
{
ptr
=
fopen
(
"client.txt"
,
"w"
);
}
fscanf
(
ptr
,
"%f"
,
&
tput
);
fclose
(
ptr
);
ptr
=
fopen
(
"client.txt"
,
"w"
);
tput
+=
(
float
)
number_of_requests
/
(
float
)
delta_us
;
fprintf
(
ptr
,
"%f"
,
tput
);
fclose
(
ptr
);
return
NULL
;
}
}
// char buffer[100];
// while(fgets(buffer, 100, stdin) != NULL) {
// write(sockfd, buffer, strlen(buffer));
// char recvBuffer[1000];
// int len = read(sockfd, recvBuffer, 999);
// recvBuffer[len] = '\0';
// printf("%s\n", recvBuffer);
// }
}
int
GET
(
char
*
key
,
char
*
request
)
{
// printf("GET called!\n");
// char request[513];
memset
(
request
,
'0'
,
513
);
request
[
0
]
=
'1'
;
// memcpy(request+1, key, strlen(key));
memcpy
(
request
+
257
-
strlen
(
key
),
key
,
strlen
(
key
));
// printf("request : %s\n", request);
}
int
PUT
(
char
*
key
,
char
*
value
,
char
*
request
)
{
// printf("PUT called!\n");
// char request[513];
memset
(
request
,
'0'
,
513
);
request
[
0
]
=
'2'
;
// memcpy(request+1, key, strlen(key));
memcpy
(
request
+
257
-
strlen
(
key
),
key
,
strlen
(
key
));
memcpy
(
request
+
513
-
strlen
(
value
),
value
,
strlen
(
value
));
// printf("key at index: %ld\n", 257-strlen(key));
// printf("value at index: %ld\n", 513-strlen(value));
}
int
DELETE
(
char
*
key
,
char
*
request
)
{
// char request[513];
// printf("DELETE called!\n");
memset
(
request
,
'0'
,
513
);
request
[
0
]
=
'3'
;
// memcpy(request+1, key, strlen(key));
memcpy
(
request
+
257
-
strlen
(
key
),
key
,
strlen
(
key
));
// printf("%s\n", request);
}
void
terminateConnection
(
char
*
request
)
{
//char request[513];
// printf("Termination function called\n");
memset
(
request
,
'0'
,
513
);
request
[
0
]
=
'4'
;
request
[
512
]
=
'\0'
;
}
int
main
(
int
argc
,
char
**
argv
)
{
printf
(
"IP = %s, Port = %s
\n
"
,
argv
[
1
],
argv
[
2
]);
// PUT("100", "200");
connectToServer
(
argv
[
1
],
argv
[
2
]);
return
0
;
}
client.cpp
View file @
60e210f4
...
...
@@ -62,18 +62,6 @@ void sendToServer(int sockfd) {
terminateConnection
(
buffer
);
break
;
}
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
514
];
int
len
=
read
(
sockfd
,
recvBuffer
,
513
);
recvBuffer
[
len
]
=
'\0'
;
if
(
recvBuffer
[
0
]
==
(
char
)
200
)
{
cout
<<
"Operation Successful"
<<
endl
;
}
else
{
cout
<<
"Operation Unsuccessful"
<<
endl
;
}
printf
(
"%s
\n
"
,
&
recvBuffer
[
1
]);
if
(
choice
==
4
)
{
gettimeofday
(
&
stop
,
NULL
);
...
...
@@ -95,6 +83,17 @@ void sendToServer(int sockfd) {
fclose
(
ptr
);
return
;
}
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
514
];
int
len
=
read
(
sockfd
,
recvBuffer
,
513
);
recvBuffer
[
len
]
=
'\0'
;
if
(
recvBuffer
[
0
]
==
(
char
)
200
)
{
cout
<<
"Operation Successful"
<<
endl
;
}
else
{
cout
<<
"Operation Unsuccessful"
<<
endl
;
}
printf
(
"%s
\n
"
,
&
recvBuffer
[
1
]);
number_of_requests
++
;
}
}
...
...
graph.png
0 → 100644
View file @
60e210f4
25.1 KB
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