From f1cdb162b14c210f868df09f2a8a3ad850d1bad9 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Tue, 22 Oct 2019 20:15:04 +0200 Subject: [PATCH] * added net_test.c --- net/net_test.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 net/net_test.c diff --git a/net/net_test.c b/net/net_test.c new file mode 100644 index 0000000..78c29d7 --- /dev/null +++ b/net/net_test.c @@ -0,0 +1,144 @@ + +#include +#include +#include +#include +#include +#include +#include + +#include "net.h" + +#define LOG(...) fprintf(stderr, __VA_ARGS__) + +#define LOG_E(...) do{ \ + LOG("%s:%s@%d: ", __FILE__, __func__, __LINE__); \ + LOG(__VA_ARGS__); \ + LOG(": %s\n", strerror(errno)); \ + }while(0) + +#define LOG_NET(e_,...) do{ \ + char ebuf[100]; \ + LOG("%s:%s@%d: ", __FILE__, __func__, __LINE__); \ + LOG(__VA_ARGS__); \ + LOG(": %s\n", net_strerror(e_,ebuf,sizeof ebuf)); \ + }while(0) + + +int main(int argc, char *argv[]) { + int n, c, l; + char buf[4000]; + + signal(SIGPIPE, SIG_IGN); + + if (argc < 2) { + LOG("Usage: %s t|T|u|U\n", argv[0]); + exit(EXIT_FAILURE); + } + + switch (*argv[1]) { + default: + case 't': + // TCP client + // Remote: ncat -l localhost 12345 + c = tcp_open_client("localhost", "12345", "localhost"); + if (c < 0) { + LOG_NET(c, "tcp_open_client"); + exit(EXIT_FAILURE); + } + while (1) { + sprintf(buf, "hello %ld\n", time(NULL)); + n = send(c, buf, strlen(buf), 0); + if (n <= 0) { + LOG_E("send"); + break; + } + if (getchar() == 'q') + break; + } + c = net_close(c); + LOG_NET(c, "net_close"); + break; + + case 'T': + // TCP server + // Remote: ncat localhost 12345 + l = tcp6_open_server("", "12345"); + if (l < 0) { + LOG_NET(l, "tcp6_open_server"); + exit(EXIT_FAILURE); + } + while (1) { + c = tcp_accept(l, 5000); + LOG_NET(c, "tcp_accept"); + if (c < 0) { + continue; + } + memset(buf, 0, sizeof buf); + n = recv(c, buf, sizeof buf-1, 0); + if (n > 0) { + printf("%s", buf); + n = send(c, buf, n, 0); + if (n <= 0) { + LOG_E("send"); + break; + } + } + else if (n < 0) { + LOG_E("recv"); + break; + } + c = net_close(c); + LOG_NET(c, "net_close"); + } + c = net_close(c); + LOG_NET(c, "net_close"); + break; + + case 'u': + // UDP client + // Remote: ncat -4 -u -l localhost 12345 + c = udp4_open_client(NULL, "12345", NULL); + if (c < 0) { + LOG_NET(c, "udp4_open_client"); + exit(EXIT_FAILURE); + } + while (1) { + sprintf(buf, "hello %ld\n", time(NULL)); + n = send(c, buf, strlen(buf), 0); + if (n <= 0) { + LOG_E("send"); + break; + } + sleep(1); + } + c = net_close(c); + LOG_NET(c, "net_close"); + break; + + case 'U': + // UDP server + // Remote: ncat -4 -u localhost 12345 + l = udp4_open_server(NULL, "12345"); + if (l < 0) { + LOG_NET(l, "udp4_open_server"); + exit(EXIT_FAILURE); + } + while (1) { + memset(buf, 0, sizeof buf); + n = recv(l, buf, sizeof buf-1, 0); + if (n > 0) { + printf("%s", buf); + } + else { + LOG_E("recv"); + break; + } + } + l = net_close(l); + LOG_NET(l, "net_close"); + break; + } + + exit(EXIT_SUCCESS); +} -- 2.30.2