summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-26 08:39:08 +0200
committerCarlos Maiolino <[email protected]>2025-09-26 08:39:08 +0200
commitfd313dd5ad9ac067a31f2b1760b85bd305567131 (patch)
tree4b539d83a679326adda5c9287f1629e78c533f08
parent7abf21eb77067191ededd96e0b0a3ddb31317eeb (diff)
netplay: small program to play with net sockets
Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--C/HF/netplay.c44
1 files changed, 44 insertions, 0 deletions
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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+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;
+}