summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-05-26 20:22:54 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-05-26 20:22:54 +0200
commitd36feb580c4cc36426a8f2de8f9ca1cd977766c2 (patch)
tree38285cec110fd9d0bf7fda5d1ff192b1c354adff
parentmake fd states more explicit (diff)
test(server|client) implement mode B
-rw-r--r--contrib/testclient.c159
-rw-r--r--contrib/testserver.c120
2 files changed, 202 insertions, 77 deletions
diff --git a/contrib/testclient.c b/contrib/testclient.c
index a5300ac..2c5324b 100644
--- a/contrib/testclient.c
+++ b/contrib/testclient.c
@@ -31,6 +31,7 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
@@ -38,8 +39,8 @@
int main(int argc, char* argv[])
{
- if(argc < 3) {
- fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
+ if(argc < 4) {
+ fprintf(stderr, "Usage: %s <addr> <port> (A|B)\n", argv[0]);
return -1;
}
@@ -67,67 +68,123 @@ int main(int argc, char* argv[])
return -1;
}
- char buf[1234567];
- unsigned int i;
- for(i = 0; i<sizeof(buf); ++i) {
- buf[i] = 'A' + i%62;
- }
- buf[sizeof(buf)-1] = '\n';
+ switch(toupper(argv[3][0])) {
+/*************** MODE A ***************/
+ case 'A': {
+ printf("MODE: A\n");
- int nbwritten = send(c, buf, sizeof(buf), 0);
- if(nbwritten <= 0) {
- if(nbwritten < 0)
- perror("send()");
- else
- fprintf(stderr, "nothing sent... aborting\n");
- return -1;
- }
- printf("%d bytes sent\n", nbwritten);
- if(nbwritten != sizeof(buf)) {
- fprintf(stderr, "to few bytes sent ... aborting!");
- return -1;
- }
-
- shutdown(c, SHUT_WR);
-
- int len = 0;
- for(;;) {
- int nbread = recv(c, &(buf[len]), sizeof(buf) - len, 0);
- if(nbread <= 0) {
- if(!nbread) {
- fprintf(stderr, "connection closed\n");
- } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
- perror("recv()");
- } else
- continue;
+ char buf[1234567];
+ unsigned int i;
+ for(i = 0; i<sizeof(buf); ++i) {
+ buf[i] = 'A' + i%62;
+ }
+ buf[sizeof(buf)-1] = '\n';
- return -1;
+ int wtot = 0;
+ for(;;) {
+ int nbwritten = send(c, &(buf[wtot]), sizeof(buf) - wtot, 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0)
+ perror("send()");
+ else
+ fprintf(stderr, "nothing sent... aborting\n");
+ return -1;
+ }
+ wtot += nbwritten;
+ printf("%d bytes sent, total=%d\n", nbwritten, wtot);
+ if(wtot >= sizeof(buf)) {
+ break;
+ }
}
- len += nbread;
- printf("%d bytes received, total = %d, left = %d", nbread, len, len - nbwritten);
- if(len == nbwritten) {
- printf(" .. finished\n");
- for(i = 0; i<nbwritten-1; ++i) {
- if(buf[i] != ('A' + i%62)) {
- fprintf(stderr, "error at index %d (expected 0x%02X, got 0x%02X)\n", i, 'A' + i%62, (unsigned int)(buf[i]));
+ shutdown(c, SHUT_WR);
+
+ int rtot = 0;
+ for(;;) {
+ int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
+ } else
+ continue;
+
+ return -1;
+ }
+
+ rtot += nbread;
+ printf("%d bytes received, total = %d, left = %d", nbread, rtot, rtot - wtot);
+ if(rtot == wtot) {
+ printf(" .. finished\n");
+ for(i = 0; i<wtot-1; ++i) {
+ if(buf[i] != ('A' + i%62)) {
+ fprintf(stderr, "error at index %d (expected 0x%02X, got 0x%02X)\n", i, 'A' + i%62, (unsigned int)(buf[i]));
+ return -1;
+ }
+ }
+ if(buf[wtot-1] != '\n') {
+ fprintf(stderr, "string doesn't end with new-line\n");
return -1;
}
- }
- if(buf[nbwritten-1] != '\n') {
- fprintf(stderr, "string doesn't end with new-line\n");
+ break;
+ } else if(rtot > wtot) {
+ fprintf(stderr, "got too many bytes back???\n");
return -1;
}
- break;
- } else if(len > nbwritten) {
- fprintf(stderr, "got too many bytes back???\n");
- return -1;
+
+ printf("\n");
}
- printf("\n");
+ while(recv(c, buf, 1, 0) > 0);
+ break;
}
+/*************** MODE A ***************/
+ case 'B': {
+ printf("MODE: B\n");
+
+ char request[] = "hello world\n";
+ int nbwritten = send(c, request, sizeof(request), 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0)
+ perror("send()");
+ else
+ fprintf(stderr, "nothing sent... aborting\n");
+ return -1;
+ }
+ printf("%d bytes sent\n", nbwritten);
+ if(nbwritten != sizeof(request)) {
+ fprintf(stderr, "to few bytes sent ... aborting!");
+ return -1;
+ }
- while(recv(c, buf, 1, 0) > 0);
+ char buf[1000];
+ int rtot = 0;
+ for(;;) {
+ int nbread = recv(c, buf, sizeof(buf), 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ return 0;
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
+ return -1;
+ }
+ continue;
+ }
+
+ rtot += nbread;
+ printf("%d bytes received, total = %d", nbread, rtot);
+ if(rtot >= 1234567) {
+ printf(" .. finished\n");
+ return 0;
+ }
+ printf("\n");
+ }
+ break;
+ }
+/**************************************/
+ }
return 0;
}
diff --git a/contrib/testserver.c b/contrib/testserver.c
index d7dca72..3200379 100644
--- a/contrib/testserver.c
+++ b/contrib/testserver.c
@@ -31,6 +31,7 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
@@ -38,8 +39,8 @@
int main(int argc, char* argv[])
{
- if(argc < 2) {
- fprintf(stderr, "Usage: %s <port>\n", argv[0]);
+ if(argc < 3) {
+ fprintf(stderr, "Usage: %s <port> (A|B)\n", argv[0]);
return -1;
}
@@ -72,6 +73,8 @@ int main(int argc, char* argv[])
return -1;
}
+ printf("MODE: %c, listening...\n", toupper(argv[2][0]));
+
struct sockaddr_in caddr;
socklen_t len = sizeof(struct sockaddr_in);
int c = accept(s, (struct sockaddr*)(&caddr), &len);
@@ -87,41 +90,106 @@ int main(int argc, char* argv[])
printf("connection from %s:%d\n", addr_str, caddr.sin_port);
- char buf[10000];
- int rtot = 0;
- for(;;) {
- int nbread = recv(c, buf, sizeof(buf), 0);
- if(nbread <= 0) {
- if(!nbread) {
- fprintf(stderr, "connection closed\n");
- sleep(1);
- return 0;
- } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
- perror("recv()");
- return -1;
+ switch(toupper(argv[2][0])) {
+/*************** MODE A ***************/
+ case 'A': {
+ char buf[10000];
+ int rtot = 0;
+ for(;;) {
+ int nbread = recv(c, buf, sizeof(buf), 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ sleep(1);
+ return 0;
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
+ return -1;
+ }
+ continue;
}
- continue;
- }
- rtot += nbread;
- printf("%d bytes received, total = %d\n", nbread, rtot);
+ rtot += nbread;
+ printf("%d bytes received, total = %d\n", nbread, rtot);
- int len = 0;
+ int len = 0;
+ for(;;) {
+ int nbwritten = send(c, &(buf[len]), nbread - len, 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0 && (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
+ perror("send()");
+ return -1;
+ }
+ continue;
+ }
+ printf("%d bytes sent\n", nbwritten);
+ len += nbwritten;
+ if(len >= nbread) {
+ break;
+ }
+ }
+ }
+ break;
+ }
+/*************** MODE A ***************/
+ case 'B': {
+ char buf[1234567];
+ int rtot = 0;
for(;;) {
- int nbwritten = send(c, &(buf[len]), nbread - len, 0);
- if(nbwritten <= 0) {
- if(nbwritten < 0 && (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
- perror("send()");
+ int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ return 0;
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
return -1;
}
continue;
}
- printf("%d bytes sent\n", nbwritten);
- len += nbwritten;
- if(len >= nbread) {
+
+ rtot += nbread;
+ printf("%d bytes received, total = %d\n", nbread, rtot);
+ unsigned char* end = memchr(buf, '\n', rtot);
+ if(!end) {
+ if(rtot >= sizeof(buf)) {
+ printf("Request too big...\n");
+ return -2;
+ }
+ } else {
+ *end = 0;
+ printf("Got Request: '%s'\n", buf);
+ break;
+ }
+ }
+
+ unsigned int i;
+ for(i = 0; i<sizeof(buf); ++i) {
+ buf[i] = 'A' + i%62;
+ }
+ buf[sizeof(buf)-1] = '\n';
+
+ int wtot = 0;
+ for(;;) {
+ int nbwritten = send(c, &(buf[wtot]), sizeof(buf) - wtot, 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0)
+ perror("send()");
+ else
+ fprintf(stderr, "nothing sent... aborting\n");
+ return -1;
+ }
+ wtot += nbwritten;
+ printf("%d bytes sent, total=%d\n", nbwritten, wtot);
+ if(wtot >= sizeof(buf)) {
break;
}
}
+ while(recv(c, buf, 1, 0) > 0);
+
+ break;
+ }
+/**************************************/
}
return 0;