-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_client.c
135 lines (103 loc) · 2.53 KB
/
game_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
#define _XOPEN_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <fcntl.h>
#include "dictionary.h"
#include "construct_word_dict.h"
#include "word_checker.h"
#ifndef PORT
#define PORT 53515
#endif
#define QUEUE_LENGTH 5
#define BUF_LENGTH 1000
#define INTERVAL 120 //in second
char buf[BUF_LENGTH];
int soc;
void sig_handler(int signo)
{
if (signo == SIGALRM){
for(int i = 0; i < BUF_LENGTH; i++){
buf[i] = '\0';
}
read(soc, buf, sizeof(buf));
printf("%s\n", buf);
alarm(INTERVAL);
}
}
void set_alarm(time_t starttime){
struct sigaction sa;
sa.sa_handler = sig_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, NULL);
time_t currenttime = time(NULL);
int diff = currenttime - starttime;
printf("time diff is %d\n", diff);
int trueINTERVAL = INTERVAL - diff;
alarm(trueINTERVAL);
}
int main(int argc, char** argv) {
struct sockaddr_in peer;
if ((soc = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("randclient: socket");
exit(1);
}
peer.sin_family = AF_INET;
peer.sin_port = htons(PORT);
if (inet_pton(AF_INET, argv[1], &peer.sin_addr) < 1) {
perror("randclient: inet_pton");
close(soc);
exit(1);
}
if (connect(soc, (struct sockaddr *)&peer, sizeof(peer)) == -1) {
perror("randclient: connect");
exit(1);
}
time_t starttime;
time_t *start_pt = &starttime;
if(read(soc, start_pt, sizeof(start_pt)) == -1){
perror("read start time");
exit(1);
}
set_alarm(starttime);
char m[1000];
char name[100];
char command[100];
read(soc, m, sizeof(m));
printf("%s", m);
scanf("%s", name);
write(soc, name, sizeof(name));
//printf("here should print welcom go ahead message: \n");
read(soc, buf, sizeof(buf));
printf("%s\n", buf);
//printf("here we submit first command: \n");
scanf("%s", command);
write(soc, command, sizeof(command));
while(strcmp(command, "quit") != 0){
for(int i = 0; i < BUF_LENGTH; i++){
buf[i] = '\0';
}
read(soc, buf, sizeof(buf));
printf("%s\n", buf);
for(int i = 0; i < 100; i++){
command[i] = '\0';
}
printf("Please give your input: \n");
scanf("%s", command);
write(soc, command, sizeof(command));
}
close(soc);
return 0;
}