diff options
| author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-21 00:16:33 +0200 | 
|---|---|---|
| committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-21 00:24:23 +0200 | 
| commit | b42d245a5c408e487f132a7ee84c9f4627c5b889 (patch) | |
| tree | a91762f3c841f5c14bfb4470c4671c2722948e46 /recv.c | |
| parent | d4e7c6876eed2733a2678668bdcabdd87659e826 (diff) | |
Added minimal send implementation
Renamed minirecv.c to recv.c
Added build commands to build.sh
Added the use of common code
Diffstat (limited to 'recv.c')
| -rw-r--r-- | recv.c | 57 | 
1 files changed, 57 insertions, 0 deletions
@@ -0,0 +1,57 @@ +// Minimal server implementation for probing out things + +#include "common.h" +#include <arpa/inet.h> +#include <errno.h> +#include <poll.h> +#include <sys/socket.h> +#include <unistd.h> + +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; +}  | 
