-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
327 lines (281 loc) · 6.23 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close
#include <netdb.h>
#include <string.h>
#include "UI.h"
#include "clientP2P.h"
#include "clientPeer1.h"
#include "clientPeer2.h"
#define clear() printf("\033[H\033[J")
#define MAX 80
#define SA struct sockaddr
int loginTime = 3;
char name[20] = "";
void login(int sockfd)
{
char buff[MAX], username[MAX];
int n;
bzero(buff, sizeof(buff));
if (loginTime != 4)
{
printf("Enter username : ");
strcat(buff, "1");
n = 1;
while ((buff[n++] = getchar()) != '\n')
;
strcpy(username, buff);
buff[n - 1] = '~';
printf("Enter password : ");
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
if (loginTime < 4)
if (strcmp(buff, "Login successfully") != 0)
loginTime--;
else
{
strcpy(name, username);
for (int i = 0; i <= strlen(name); i++)
name[i] = name[i + 1];
name[strlen(name) - 1] = '\0';
loginTime = 4;
}
if (loginTime == 0)
{
username[0] = '3';
write(sockfd, username, sizeof(username));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
}
printf("From Server : %s\n", buff);
}
else
{
printf("You already login !\n");
}
}
void signup(int sockfd)
{
char buff[MAX], username[MAX];
int n;
bzero(buff, sizeof(buff));
printf("Enter username : ");
strcat(buff, "4"); //set for logout
n = 1;
while ((buff[n++] = getchar()) != '\n')
;
strcpy(username, buff);
buff[n - 1] = '~';
printf("Enter password : ");
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
if (strcmp(buff, "Signup successfully") == 0)
{
strcpy(name, username);
for (int i = 0; i <= strlen(name); i++)
name[i] = name[i + 1];
name[strlen(name) - 1] = '\0';
loginTime = 4;
}
printf("From Server : %s\n", buff);
}
void logout(int sockfd)
{
char buff[MAX];
int n;
bzero(buff, sizeof(buff));
printf("Enter username : ");
strcat(buff, "2");
n = 1;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
if (strcmp(buff, "Goodbye") == 0)
loginTime = 3;
printf("From Server : %s\n", buff);
}
void normalgame(int sockfd)
{
int a, iPort;
char buff[MAX], *ip_port, *ip, *cPort;
bzero(&buff, sizeof(buff));
ip_port = (char *)calloc(80, sizeof(char));
ip = (char *)calloc(20, sizeof(char));
cPort = (char *)calloc(10, sizeof(char));
strcpy(ip_port, genPort());
strcat(buff, "5");
strcat(buff, ip_port);
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
if (buff[0] == 'h')
{
for (int i = 0; i < strlen(buff); i++)
buff[i] = buff[i + 1];
strcpy(ip, buff);
iPort = atoi(ip_port);
startP2P(ip, iPort, 1, name, sockfd);
}
else
{
strcat(ip, return_ip(buff));
strcat(cPort, return_port(buff));
iPort = atoi(cPort);
connectP2P(ip, iPort, 1, name, sockfd);
}
}
void rankgame(int sockfd, char name[])
{
int a, iPort;
char buff[MAX], *ip_port, *ip, *cPort;
bzero(&buff, sizeof(buff));
ip_port = (char *)calloc(80, sizeof(char));
ip = (char *)calloc(20, sizeof(char));
cPort = (char *)calloc(10, sizeof(char));
strcpy(ip_port, genPort());
strcat(buff, "6");
strcat(buff, ip_port);
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
if (buff[0] == 'h')
{
for (int i = 0; i < strlen(buff); i++)
buff[i] = buff[i + 1];
strcpy(ip, buff);
iPort = atoi(ip_port);
startP2P(ip, iPort, 2, name, sockfd);
}
else
{
strcat(ip, return_ip(buff));
strcat(cPort, return_port(buff));
iPort = atoi(cPort);
connectP2P(ip, iPort, 2, name, sockfd);
}
}
void showRank(int sockfd, char name[])
{
int a;
char buff[MAX], rank[500];
bzero(buff, sizeof(buff));
strcat(buff, "7");
strcat(buff, name);
write(sockfd, buff, sizeof(buff));
// bzero(buff, sizeof(buff));
read(sockfd, rank, sizeof(rank));
rankNav();
printf("%s", rank);
getchar();
}
void gameScreen(int sockfd)
{
char d;
while (1)
{
clear();
gameMenu(name);
scanf("%c", &d);
if (d == '1')
{
scanf("%*c");
normalgame(sockfd);
}
if (d == '2')
{
scanf("%*c");
rankgame(sockfd, name);
}
if (d == '3')
{
scanf("%*c");
showRank(sockfd, name);
}
if (d == '4')
{
scanf("%*c");
clear();
info(name);
}
if (d == 'q')
{
scanf("%*c");
logout(sockfd);
break;
}
}
}
int main(int argc, char *argv[])
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
char ipAddress[15];
unsigned short PORT;
if (argc < 2 || argc > 3)
{
printf("hay nhap chuoi theo cu phap \n ./client 3asd15 \n ");
return 0;
}
strcpy(ipAddress, argv[1]);
PORT = atoi(argv[2]);
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(ipAddress);
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
char c, d;
int check = 0;
while (1)
{
if (loginTime >= -1)
{
if (loginTime == 4)
gameScreen(sockfd);
clear();
loginMenu();
scanf("%c", &c);
if (c == '1')
{
scanf("%*c");
login(sockfd);
}
if (c == '2')
{
scanf("%*c");
signup(sockfd);
}
if (c != '2' && c != '1')
break;
}
}
// close the socket
close(sockfd);
}