-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientPeer1.c
186 lines (160 loc) · 4.16 KB
/
clientPeer1.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
#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"
#define MAX 80
#define SA struct sockaddr
#define clear() printf("\033[H\033[J")
void hostPerson(int sockfd, int typeOfGame, char name[], int connectserver)
{
strcpy(pointBroad, "000000000");
char buff[MAX], msg[MAX], competitorName[MAX];
int n;
bzero(buff, MAX);
bzero(competitorName, MAX);
if (typeOfGame == 2)
{
strcpy(buff, name);
write(sockfd, buff, sizeof(buff));
read(sockfd, competitorName, sizeof(competitorName));
}
// infinite loop for game
for (;;)
{
bzero(buff, MAX);
clear();
ingame(pointBroad);
if (typeOfGame == 2)
score(name, competitorName);
printf("Doi doi phuong danh ...\n");
// read the message from client and copy it in buffer
if (read(sockfd, buff, sizeof(buff)) == 0)
{
if (typeOfGame == 2)
{
bzero(msg, MAX);
strcat(msg, "6~");
strcat(msg, name);
strcat(msg, "~");
strcat(msg, competitorName);
write(connectserver, msg, sizeof(msg));
read(connectserver, msg, sizeof(msg));
bzero(msg, MAX);
printf("%s", msg);
}
printf("\n Ban da chien thang do nguoi choi kia da bo cuoc !!!");
getchar();
close(sockfd);
break;
}
// print buffer which contains the client contents
strcpy(pointBroad, updateBroad(pointBroad, buff, '2'));
clear();
ingame(pointBroad);
if (typeOfGame == 2)
score(name, competitorName);
if (checkWinner(pointBroad, '2'))
{
bzero(buff, MAX);
printf("Ban da thua cuoc !!!");
getchar();
close(sockfd);
break;
}
// copy server message in the buffer
printf("Nhap vi tri muon danh : ");
while (1)
{
bzero(buff, MAX);
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
if (!isPositionExits(buff))
break;
}
if (strcmp(buff, "q\n") == 0)
{
close(sockfd);
break;
}
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
strcpy(pointBroad, updateBroad(pointBroad, buff, '1'));
if (checkWinner(pointBroad, '1'))
{
printf("Ban da chien thang !!!");
if (typeOfGame == 2)
{
bzero(msg, MAX);
strcat(msg, "6~");
strcat(msg, name);
strcat(msg, "~");
strcat(msg, competitorName);
write(connectserver, msg, sizeof(msg));
read(connectserver, msg, sizeof(msg));
bzero(msg, MAX);
printf("%s", msg);
}
getchar();
close(sockfd);
break;
}
}
}
void startP2P(char ip[], int PORT, int typeOfGame, char name[], int connectserver)
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
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(ip);
servaddr.sin_port = htons(PORT);
printf("ip port: %s:%d___", ip, PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA *)&servaddr, sizeof(servaddr))) != 0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0)
{
printf("Listen failed...\n");
exit(0);
}
else
printf("Waiting another player ...\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA *)&cli, &len);
if (connfd < 0)
{
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
// typeOfGame ==1 -> nomarl game
// typeOfGame ==2 -> rank game
hostPerson(connfd, typeOfGame, name, connectserver);
close(sockfd);
}