summaryrefslogtreecommitdiff
path: root/pd_server.c
blob: b8ff6f02cdb0f78620f218ecef4a046b33e7068b (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

enum {
	COORPERATION = 'c',
	DEFECTION = 'd'
}; 

int pay(int a, int b) 
{
	assert(a == COORPERATION || a == DEFECTION);
	assert(b == COORPERATION || b == DEFECTION);

	if (a == COORPERATION && b == DEFECTION)    return 1; 
	if (a == DEFECTION    && b == DEFECTION)    return 2; 
	if (a == COORPERATION && b == COORPERATION) return 3; 
	if (a == DEFECTION    && b == COORPERATION) return 4; 

	exit(EXIT_FAILURE); 
}

int
main(int argc, char *argv[])
{
	int sock, player[2];
	struct sockaddr_in serv_addr;
	int port = 8068;
	int a_payment = 0, b_payment = 0, rounds, i;
	int optval = 1;
	char *endptr;

	errno = 0;
	if (argc < 2 || (rounds = strtol(argv[1], NULL, 10)) <= 0 || errno!=0) {
		fprintf(stderr, "usage: %s number-of-rounds\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		return EXIT_FAILURE;
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
		   &optval, sizeof optval);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(port);
	if (bind(sock, (struct sockaddr *) &serv_addr,
		 sizeof(serv_addr)) < 0) {
		fprintf(stderr, "Failed to bind: %s\n",
			strerror(errno));
		return EXIT_FAILURE;
	}
	listen(sock, 2);

	for (;;) {
		for (i = 0; i < 2; ++i) {
			player[i] = accept(sock, NULL, NULL);
		}
		for (i = 0; i < rounds; ++i) {
			char a, b;
			read(player[0], &a, 1);
			read(player[1], &b, 1);

			if (a != 'c' && a != 'd') {
				printf("a sent incorrect char: %c\n", a);
				continue;
			}
			if (b != 'c' && b != 'd') {
				printf("b sent incorrect char: %c\n", b);
				continue;
			}

			a_payment += pay(a, b);
			b_payment += pay(b, a);

			printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\t"
			       "A: %d\tB: %d\n",
			       a, pay(a,b), b, pay(b,a), a_payment, b_payment);

			write(player[0], &b, 1);
			write(player[1], &a, 1);
		}
		close(player[0]);
		close(player[1]);
	}
	close(sock);
}