Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Maxime POULAIN
Réseau - Client Serveur
Commits
5e108d86
Commit
5e108d86
authored
Nov 16, 2021
by
Maxime POULAIN
Browse files
fix server crashing at client exit
parent
09fff080
Changes
2
Hide whitespace changes
Inline
Side-by-side
server/server
View file @
5e108d86
No preview for this file type
server/server.c
View file @
5e108d86
...
...
@@ -10,8 +10,6 @@
#include "bool.h"
void
start
(
int
port
)
{
bool
needToStop
=
false
;
int
server_fd
,
new_socket
,
valread
;
struct
sockaddr_in
address
;
int
opt
=
1
;
...
...
@@ -44,21 +42,33 @@ void start(int port) {
exit
(
EXIT_FAILURE
);
}
printf
(
"Listening on port %d
\n
"
,
port
);
if
((
new_socket
=
accept
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
(
socklen_t
*
)
&
addrlen
))
<
0
)
{
perror
(
"accept"
);
exit
(
EXIT_FAILURE
);
}
while
(
!
needToStop
)
{
memset
(
bufferIn
,
0
,
BUFFER_SIZE
);
memset
(
bufferOut
,
0
,
BUFFER_SIZE
);
recv
(
new_socket
,
bufferIn
,
BUFFER_SIZE
,
0
);
handleClientRequest
(
bufferIn
,
bufferOut
);
printf
(
"CLIENT : %s
\n
"
,
bufferIn
);
printf
(
"SERVER : %s
\n
"
,
bufferOut
);
send
(
new_socket
,
bufferOut
,
BUFFER_SIZE
,
0
);
while
(
true
)
{
if
((
new_socket
=
accept
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
(
socklen_t
*
)
&
addrlen
))
<
0
)
{
perror
(
"accept"
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Client connected
\n
"
);
while
(
true
)
{
memset
(
bufferIn
,
0
,
BUFFER_SIZE
);
memset
(
bufferOut
,
0
,
BUFFER_SIZE
);
recv
(
new_socket
,
bufferIn
,
BUFFER_SIZE
,
0
);
if
(
strcmp
(
bufferIn
,
"exit"
)
==
0
){
break
;
}
handleClientRequest
(
bufferIn
,
bufferOut
);
printf
(
"CLIENT : %s
\n
"
,
bufferIn
);
printf
(
"SERVER : %s
\n
"
,
bufferOut
);
send
(
new_socket
,
bufferOut
,
BUFFER_SIZE
,
0
);
}
printf
(
"Client disconnected
\n\n
"
);
close
(
new_socket
);
}
close
(
server_fd
);
}
void
handleClientRequest
(
char
*
bufferIn
,
char
*
bufferOut
)
{
...
...
@@ -68,13 +78,13 @@ void handleClientRequest(char *bufferIn, char *bufferOut) {
break
;
case
'2'
:
handleClientRequestSort
(
bufferIn
,
bufferOut
);
handleClientRequestSort
(
bufferIn
,
bufferOut
);
break
;
}
}
void
handleClientRequestSize
(
char
*
bufferIn
,
char
*
bufferOut
)
{
int
len
=
strlen
(
bufferIn
)
-
2
;
int
len
=
strlen
(
bufferIn
)
-
2
;
sprintf
(
bufferOut
,
"%d"
,
len
);
}
...
...
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