fixing problems with detecting a disconnected client
This commit is contained in:
parent
496198f488
commit
be121ac22f
2 changed files with 15 additions and 14 deletions
5
client.c
5
client.c
|
@ -23,6 +23,11 @@ int client_read(struct client_t *client) {
|
||||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
/* a disconnected client socket is ready for reading but read returns 0 */
|
||||||
|
if (len == 0) {
|
||||||
|
fprintf(stderr, "[%s:%d] no data available from client\n", __func__, __LINE__);
|
||||||
|
return -ENODATA; //TODO document this in header
|
||||||
|
}
|
||||||
|
|
||||||
//TODO let's print received bytes during development phase...
|
//TODO let's print received bytes during development phase...
|
||||||
{
|
{
|
||||||
|
|
24
moxerver.c
24
moxerver.c
|
@ -23,11 +23,11 @@ static void usage() {
|
||||||
/* Performs cleanup and exit. */
|
/* Performs cleanup and exit. */
|
||||||
void cleanup(int exit_code) {
|
void cleanup(int exit_code) {
|
||||||
fprintf(stderr, "[%s] cleanup and exit with %d\n", NAME, exit_code);
|
fprintf(stderr, "[%s] cleanup and exit with %d\n", NAME, exit_code);
|
||||||
/* close server and client */
|
/* close client and server */
|
||||||
server_close(&server);
|
|
||||||
if (client.socket != -1) {
|
if (client.socket != -1) {
|
||||||
client_close(&client);
|
client_close(&client);
|
||||||
}
|
}
|
||||||
|
server_close(&server);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,6 +153,13 @@ int main(int argc, char *argv[]) {
|
||||||
if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) {
|
if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) {
|
||||||
/* read client data */
|
/* read client data */
|
||||||
ret = client_read(&client);
|
ret = client_read(&client);
|
||||||
|
/* check if client disconnected */
|
||||||
|
if (ret == -ENODATA) {
|
||||||
|
fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string);
|
||||||
|
/* close client connection and continue waiting for new clients */
|
||||||
|
client_close(&client);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if ( ret < 0) {
|
if ( ret < 0) {
|
||||||
/* print error but continue waiting for new data */
|
/* print error but continue waiting for new data */
|
||||||
fprintf(stderr, "[%s] problem reading client\n", NAME);
|
fprintf(stderr, "[%s] problem reading client\n", NAME);
|
||||||
|
@ -165,18 +172,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
/* check if client disconnected */
|
fprintf(stderr, "[%s] server waiting\n", NAME);
|
||||||
/* a disconnected client socket is ready for reading but read returns 0 */
|
|
||||||
if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) {
|
|
||||||
if (client_read(&client) == 0) {
|
|
||||||
fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string);
|
|
||||||
/* close client connection */
|
|
||||||
client_close(&client);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fprintf(stderr, "[%s] server waiting\n", NAME);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* END while() loop */
|
} /* END while() loop */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue