aboutsummaryrefslogtreecommitdiff
path: root/tests/sockets
diff options
context:
space:
mode:
authorAnthony Pesch <inolen@gmail.com>2013-08-30 16:52:52 -0700
committerAnthony Pesch <inolen@gmail.com>2013-08-30 16:53:05 -0700
commit5cf189f76e6e0ebb5d0e4866e066ac26da7ccbb6 (patch)
treebbc72b05c0854b083329e71f93c43685104f1a37 /tests/sockets
parent19d175c00a94e66f96b2535fae7b4d4fad3c2248 (diff)
don't close client sockets by default in echo server enabling TCP teardown to succeed
Diffstat (limited to 'tests/sockets')
-rw-r--r--tests/sockets/test_sockets_echo_client.c19
-rw-r--r--tests/sockets/test_sockets_echo_server.c35
-rw-r--r--tests/sockets/test_sockets_partial_server.c13
-rw-r--r--tests/sockets/test_sockets_select_server_closes_connection_client_rw.c14
4 files changed, 67 insertions, 14 deletions
diff --git a/tests/sockets/test_sockets_echo_client.c b/tests/sockets/test_sockets_echo_client.c
index 6b3ccef3..f6ea85cf 100644
--- a/tests/sockets/test_sockets_echo_client.c
+++ b/tests/sockets/test_sockets_echo_client.c
@@ -38,7 +38,10 @@ int echo_read;
int echo_wrote;
void finish(int result) {
- close(server.fd);
+ if (server.fd) {
+ close(server.fd);
+ server.fd = 0;
+ }
#if EMSCRIPTEN
REPORT_RESULT();
#endif
@@ -76,7 +79,12 @@ void main_loop(void *arg) {
assert(available);
res = do_msg_read(server.fd, &server.msg, echo_read, 0, NULL, NULL);
- if (res != -1) echo_read += res;
+ if (res == 0) {
+ perror("server closed");
+ finish(EXIT_FAILURE);
+ } else if (res != -1) {
+ echo_read += res;
+ }
// once we've read the entire message, validate it
if (echo_read >= server.msg.length) {
@@ -89,7 +97,12 @@ void main_loop(void *arg) {
}
res = do_msg_write(server.fd, &echo_msg, echo_wrote, 0, NULL, 0);
- if (res != -1) echo_wrote += res;
+ if (res == 0) {
+ perror("server closed");
+ finish(EXIT_FAILURE);
+ } else if (res != -1) {
+ echo_wrote += res;
+ }
// once we're done writing the message, read it back
if (echo_wrote >= echo_msg.length) {
diff --git a/tests/sockets/test_sockets_echo_server.c b/tests/sockets/test_sockets_echo_server.c
index f01004c3..dbc912a6 100644
--- a/tests/sockets/test_sockets_echo_server.c
+++ b/tests/sockets/test_sockets_echo_server.c
@@ -1,6 +1,7 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,8 +39,14 @@ server_t server;
client_t client;
void cleanup() {
- if (server.fd) close(server.fd);
- if (client.fd) close(client.fd);
+ if (client.fd) {
+ close(client.fd);
+ client.fd = 0;
+ }
+ if (server.fd) {
+ close(server.fd);
+ server.fd = 0;
+ }
}
void main_loop(void *arg) {
@@ -83,7 +90,13 @@ void main_loop(void *arg) {
}
res = do_msg_read(fd, &client.msg, client.read, 0, (struct sockaddr *)&client.addr, &addrlen);
- if (res != -1) client.read += res;
+ if (res == 0) {
+ // client disconnected
+ memset(&client, 0, sizeof(client_t));
+ return;
+ } else if (res != -1) {
+ client.read += res;
+ }
// once we've read the entire message, echo it back
if (client.read >= client.msg.length) {
@@ -96,12 +109,22 @@ void main_loop(void *arg) {
}
res = do_msg_write(fd, &client.msg, client.wrote, 0, (struct sockaddr *)&client.addr, sizeof(client.addr));
- if (res != -1) client.wrote += res;
+ if (res == 0) {
+ // client disconnected
+ memset(&client, 0, sizeof(client_t));
+ return;
+ } else if (res != -1) {
+ client.wrote += res;
+ }
- // close the client once we've echo'd back the entire message
if (client.wrote >= client.msg.length) {
+ client.wrote = 0;
+ client.state = MSG_READ;
+
+#if CLOSE_CLIENT_AFTER_ECHO
close(client.fd);
memset(&client, 0, sizeof(client_t));
+#endif
}
}
}
@@ -111,7 +134,7 @@ int main() {
int res;
atexit(cleanup);
- //signal(SIGTERM, cleanup);
+ signal(SIGTERM, cleanup);
memset(&server, 0, sizeof(server_t));
memset(&client, 0, sizeof(client_t));
diff --git a/tests/sockets/test_sockets_partial_server.c b/tests/sockets/test_sockets_partial_server.c
index 44ad40a3..19f7f2af 100644
--- a/tests/sockets/test_sockets_partial_server.c
+++ b/tests/sockets/test_sockets_partial_server.c
@@ -1,6 +1,7 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,8 +19,14 @@ int serverfd = 0;
int clientfd = 0;
void cleanup() {
- if (serverfd) close(serverfd);
- if (clientfd) close(clientfd);
+ if (serverfd) {
+ close(serverfd);
+ serverfd = 0;
+ }
+ if (clientfd) {
+ close(clientfd);
+ clientfd = 0;
+ }
}
void do_send(int sockfd) {
@@ -86,7 +93,7 @@ int main() {
int res;
atexit(cleanup);
- //signal(SIGTERM, cleanup);
+ signal(SIGTERM, cleanup);
// create the socket
serverfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
diff --git a/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c
index 198ad232..25dcdd05 100644
--- a/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c
+++ b/tests/sockets/test_sockets_select_server_closes_connection_client_rw.c
@@ -89,7 +89,12 @@ void main_loop(void *arg) {
// read a single byte
transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
- if (transferAmount != -1) readPos += transferAmount;
+ if (transferAmount == 0) {
+ perror("server closed");
+ finish(EXIT_FAILURE);
+ } else if (transferAmount != -1) {
+ readPos += transferAmount;
+ }
// if successfully reading 1 byte, switch to next state
if (readPos >= 1) {
@@ -122,7 +127,12 @@ void main_loop(void *arg) {
// read a single byte
transferAmount = do_msg_read(sockfd, &readmsg, readPos, 1, NULL, NULL);
- if (transferAmount != -1) readPos += transferAmount;
+ if (transferAmount == 0) {
+ perror("server closed");
+ finish(EXIT_FAILURE);
+ } else if (transferAmount != -1) {
+ readPos += transferAmount;
+ }
// with 10 bytes read the inQueue is empty => switch state
if (readPos >= readmsg.length) {