From b42d245a5c408e487f132a7ee84c9f4627c5b889 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Mon, 21 Oct 2024 00:16:33 +0200 Subject: Added minimal send implementation Renamed minirecv.c to recv.c Added build commands to build.sh Added the use of common code --- recv.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 recv.c (limited to 'recv.c') diff --git a/recv.c b/recv.c new file mode 100644 index 0000000..e4433ae --- /dev/null +++ b/recv.c @@ -0,0 +1,57 @@ +// Minimal server implementation for probing out things + +#include "common.h" +#include +#include +#include +#include +#include + +int main(void) +{ + u32 serverfd, clientfd; + u8 on = 1; + + const struct sockaddr_in address = { + AF_INET, + htons(PORT), + {0}, + }; + + serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); + if (bind(serverfd, (struct sockaddr *)&address, sizeof(address))) + return 1; + + listen(serverfd, 256); + + clientfd = accept(serverfd, 0, 0); + + struct pollfd fds[1] = { + {clientfd, POLLIN, 0}, + }; + + for (;;) { + int ret = poll(fds, 1, 50000); + if (ret == -1) + return 2; + + if (fds[0].revents & POLLIN) { + u8 recv_buf[BUF_MAX]; + u32 nrecv = recv(clientfd, recv_buf, sizeof(recv_buf), 0); + + writef("client(%d): %d bytes received.\n", clientfd, nrecv); + if (nrecv == -1) { + return errno; + } else if (nrecv == 0) { + writef("client(%d): disconnected.\n", clientfd); + fds[0].fd = -1; + fds[0].revents = 0; + close(clientfd); + return 0; + } + } + } + + return 0; +} -- cgit v1.2.3