summaryrefslogtreecommitdiff
path: root/C/HF/netplay.c
blob: 3862a90f9da3d4b34e20623bd49c33cf0ed5b944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}