aboutsummaryrefslogtreecommitdiff
path: root/receive.h
blob: f89922fff8dc6828edf33076c373535626105a55 (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
45
46
47
/**
 * Localchat
 * Ben Burwell
 *
 * Thread management functions
 */

void *receive_function(void *arg) {

	char                in_buf[256];
	struct sockaddr_in  server_addr;
	int                 addr_len;
	int                 client_sock = socket(AF_INET, SOCK_DGRAM, 0);
	int                 ret_code;

	if (client_sock < 0) {
		exit(-1);
	}

	server_addr.sin_family = AF_INET;                  // Address family to use
	server_addr.sin_port = htons(CMD_PORT);            // Port num to use
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);  // Listen on any IP address
	ret_code = bind(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));

	while (1) {

		addr_len = sizeof(server_addr);
		ret_code = recvfrom(client_sock, in_buf, sizeof(in_buf), 0, (struct sockaddr *)&server_addr, &addr_len);

		if (DEBUG) printf("Received from server: %s (%d) \n", in_buf, ret_code);
		parse_command(in_buf);
	}
	
	return;
}

void start_receive_thread() {
	
	pthread_t thread;

	if (pthread_create(&thread, NULL, receive_function, NULL) > 0) {
		printf("Error starting receive thread \n");
		exit(-1);
	}

	return;
}