-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw4_bingo_client.c
144 lines (107 loc) · 2.69 KB
/
hw4_bingo_client.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <time.h>
#define RAND_START 1
#define RAND_END 30
#define ROW 4
#define COL 4
// result field value
#define FAIL 0
#define SUCCESS 1
#define CHECKED 2
// cmd field value
#define BINGO_REQ 0
#define BINGO_RES 1
#define BINGO_END 2
// Server -> Client send packet
// this is response packet
typedef struct {
int cmd; // BINGO_RES, BINGO_END
int number; // chosen number by user
int board[ROW][COL]; // bingo state
int result; // result from server (FAIL, SUCCESS, CHECKED)
}RES_PACKET;
// Client -> Server send packet
// this is request packet
typedef struct {
int cmd; // BINGO_REQ
int number; // random number by user
}REQ_PACKET;
void error_handling(char* message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
int bingo_array[ROW][COL] = {0}; // random bingo array
int player_choice_array[ROW][COL] = {0}; // Client state
int checked[ROW][COL] = {0};
int main(int argc, char *argv[])
{
// make socket
int sock;
RES_PACKET res;
REQ_PACKET req;
socklen_t adr_sz;
struct sockaddr_in serv_adr, from_adr;
if(argc != 3)
{
printf("Usage: %s <IP> <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_DGRAM, 0);
if(sock == -1)
error_handling("socket() error");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
serv_adr.sin_port = htons(atoi(argv[2]));
srand(time(NULL));
while(1)
{
// make random number
int num = (rand() % RAND_END) + RAND_START;
printf("number: %d\n", num);
// BINGO REQ
memset(&req, 0, sizeof(req));
req.number= num;
req.cmd = BINGO_REQ;
sendto(sock, &req, sizeof(req), 0, (struct sockaddr*)&serv_adr, sizeof(serv_adr));
printf("[Tx] BINGO REQ(number: %d)\n\n", num);
adr_sz = sizeof(from_adr);
// BINGO RES
memset(&res, 0, sizeof(res));
recvfrom(sock, &res, sizeof(res), 0, (struct sockaddr*)&from_adr, &adr_sz);
if(res.cmd == BINGO_END)
printf("[Rx] BINGO END\n");
else
printf("[Rx] BINGO_RES(number: %d, result: %d)\n", res.number, res.result);
for(int i = 0; i < ROW; i++)
{
printf("-------------\n");
printf("|");
for(int j = 0; j < COL; j++)
{
if(res.board[i][j] == 0)
printf(" |");
else
printf("%2d|", res.board[i][j]);
}
printf("\n");
}
printf("-------------\n");
printf("\n");
if(res.cmd == BINGO_END)
{
printf("Exit Client\n");
break;
}
}
close(sock);
return 0;
}