Implement TCP client

This commit is contained in:
Quentin 2021-01-05 11:34:10 +01:00
parent fa02001a08
commit ec40ad0a99

View file

@ -335,6 +335,32 @@ void spawn_udp_server(struct evt_core_ctx* evts) {
printf("--- UDP server is listening\n");
}
void spawn_tcp_client(struct evt_core_ctx* evts) {
struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read");
if (ucat == NULL) {
fprintf(stderr, "Category tcp-read not found\n");
exit(EXIT_FAILURE);
}
struct measlat_ctx* mctx = ucat->app_ctx;
int tcp_sock = create_tcp_client (mctx->host, mctx->port);
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
fdinfo.url = url;
fdinfo.fd = tcp_sock;
fdinfo.cat->name = "tcp-read";
fdinfo.other = &mctx->mc;
fdinfo.free_other = NULL;
sprintf(fdinfo.url, "tcp:read:%s:%s", mctx->host, mctx->port);
evt_core_add_fd (evts, &fdinfo);
printf("--- TCP client registered\n");
register_timer(evts, NULL);
}
void spawn_tcp_server(struct evt_core_ctx* evts, uint16_t *ports) {
char buffer[1024];
int tcp_serv_sock, err;
@ -447,6 +473,7 @@ int main(int argc, char** argv) {
else if (mctx.mc.is_server && strcmp(mctx.transport, "tcp") == 0) spawn_tcp_server(&evts, ports);
else if (strcmp(mctx.transport, "udp") == 0) spawn_udp_client(&evts);
else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_client(&evts);
else if (strcmp(mctx.transport, "tcp") == 0) spawn_tcp_client(&evts);
else exit(EXIT_FAILURE);
// 4. Run main loop
@ -454,6 +481,6 @@ int main(int argc, char** argv) {
return 0;
usage:
fprintf(stderr, "Usage: %s -h <host> -p <port> [-l] [-r] [-t <udp|tor>] [-c <count>] [-i <ms>] [-s <bytes>]\n", argv[0]);
fprintf(stderr, "Usage: %s [-h <host>] [-p <port>] [-l] [-r] [-t <udp|tor>] [-c <count>] [-i <ms>] [-s <bytes>]\n", argv[0]);
exit(EXIT_FAILURE);
}