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
5e9e472b
Commit
5e9e472b
authored
Nov 10, 2020
by
Saikumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Delete server.c
parent
3aca97f5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
137 deletions
+0
-137
server.c
server.c
+0
-137
No files found.
server.c
deleted
100644 → 0
View file @
3aca97f5
#include <stdio.h>
#include "server.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
void
initVals
(
char
*
ip
,
char
*
portNo
,
int
*
nThreads
)
{
FILE
*
ptr
=
fopen
(
"config.txt"
,
"r"
);
fscanf
(
ptr
,
"%s
\n
%s
\n
%d
\n
"
,
ip
,
portNo
,
nThreads
);
fclose
(
ptr
);
}
void
*
respondToClient
(
void
*
args
)
{
int
clientFd
=
*
((
int
*
)
args
);
while
(
1
)
{
// printf("reading for client %d\n", clientFd);
// char buffer[513];
char
*
buffer
=
(
char
*
)
malloc
(
513
*
sizeof
(
char
));
int
len
=
read
(
clientFd
,
buffer
,
513
);
buffer
[
len
]
=
'\0'
;
char
key
[
100
],
value
[
100
];
memset
(
key
,
'\0'
,
100
);
memset
(
value
,
'\0'
,
100
);
char
todo
=
buffer
[
0
];
// key: first non-zero till byte 257
int
i
;
if
(
todo
!=
'4'
){
for
(
i
=
1
;
buffer
[
i
]
==
'0'
;
i
++
);
// printf("I: %d %c\n", i, buffer[i]);
printf
(
"key length: %d
\n
"
,
257
-
i
);
memcpy
(
key
,
buffer
+
i
,
257
-
i
);
// value: first non-zero till byte 513
for
(
i
=
257
;
buffer
[
i
]
==
'0'
;
i
++
);
printf
(
"value length: %d
\n
"
,
513
-
i
);
memcpy
(
value
,
buffer
+
i
,
513
-
i
);
}
switch
(
todo
)
{
case
'1'
:
printf
(
"GET recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
write
(
clientFd
,
"Get returns
\0
"
,
12
);
break
;
case
'2'
:
printf
(
"PUT recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
printf
(
"Value: %s
\n
"
,
value
);
write
(
clientFd
,
"Put returns
\0
"
,
12
);
break
;
case
'3'
:
printf
(
"DELETE recvd
\n
"
);
printf
(
"Key: %s
\n
"
,
key
);
write
(
clientFd
,
"Delete returns
\0
"
,
15
);
break
;
case
'4'
:
printf
(
"Connection terminated
\n
"
);
write
(
clientFd
,
"Connection terminated
\0
"
,
22
);
return
NULL
;
// break;
}
// printf("%s\n", buffer);
// write(clientFd, "Client response\0", 16);
// if (todo=='4')
// {
// return NULL;
// }
}
}
int
acceptConnections
(
char
*
addr
,
char
*
portNo
,
int
nThreads
)
{
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
pthread_t
clientThreads
[
nThreads
];
int
clientNo
=
0
;
struct
addrinfo
hints
,
*
result
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_flags
=
AI_PASSIVE
;
int
s
=
getaddrinfo
(
addr
,
portNo
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
return
-
1
;
}
int
b
=
bind
(
sockfd
,
result
->
ai_addr
,
result
->
ai_addrlen
);
if
(
b
!=
0
)
{
printf
(
"Binding error
\n
"
);
return
-
1
;
}
int
l
=
listen
(
sockfd
,
nThreads
);
if
(
l
!=
0
)
{
printf
(
"Error while listening...
\n
"
);
return
-
1
;
}
printf
(
"Waiting for connection...
\n
"
);
while
(
1
)
{
int
clientFd
=
accept
(
sockfd
,
NULL
,
NULL
);
printf
(
"Connected to client with fd: %d
\n
"
,
clientFd
);
clientNo
++
;
// shared variables hence we need conditional variable
int
fid
=
fork
();
if
(
fid
==
0
)
{
// create thread
int
*
cfd
=
&
clientFd
;
pthread_create
(
&
clientThreads
[
clientNo
],
NULL
,
respondToClient
,
(
void
*
)
cfd
);
}
else
{
// manage threads
}
}
return
0
;
}
int
main
(
int
argc
,
char
**
argv
)
{
printf
(
"Server is running...
\n
"
);
char
ip
[
20
],
portNo
[
10
];
int
nThreads
;
initVals
(
ip
,
portNo
,
&
nThreads
);
printf
(
"Ip address: %s
\n
"
,
ip
);
printf
(
"portno: %s
\n
"
,
portNo
);
printf
(
"no of threads: %d
\n
"
,
nThreads
);
acceptConnections
(
ip
,
portNo
,
nThreads
);
return
0
;
}
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