From fd313dd5ad9ac067a31f2b1760b85bd305567131 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Fri, 26 Sep 2025 08:39:08 +0200 Subject: netplay: small program to play with net sockets Signed-off-by: Carlos Maiolino --- C/HF/netplay.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 C/HF/netplay.c (limited to 'C/HF') diff --git a/C/HF/netplay.c b/C/HF/netplay.c new file mode 100644 index 0000000..3862a90 --- /dev/null +++ b/C/HF/netplay.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include + +char *advice[] = { + "Take the hard path\r\n", + "Don't please others\r\n", + "Ignore others opinions\r\n", + "Keep moving forward\r\n", +}; + +int +main(void) { + + int sock_d = socket(PF_INET, SOCK_STREAM, 0); + + struct sockaddr_in name; + name.sin_family = PF_INET; + name.sin_port = (in_port_t)htons(30000); + name.sin_addr.s_addr = htonl(INADDR_ANY); + + bind(sock_d, (struct sockaddr *)&name, sizeof(name)); + + listen(sock_d, 10); + + puts("Waiting connection...\n"); + + while (1) { + struct sockaddr_storage client_addr; + unsigned int addr_size = sizeof(client_addr); + int client_sock = accept(sock_d, (struct sockaddr *)&client_addr, + &addr_size); + + char *msg = advice[rand() % 4]; + + send(client_sock, msg, strlen(msg), 0); + close(client_sock); + } + + return 0; +} -- cgit v1.2.3