-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cpp
240 lines (174 loc) · 4.9 KB
/
Network.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "BaseApplication.h"
using std::cout;
using std::hex;
Network::Network(int state,BaseApplication* bap):
connected(0),
baseapp(bap)
{
//1: server, 2: client, 3: single
if (state == 1) {
curState = GAME_SERVER;
}
else if (state == 2) {
curState = GAME_CLIENT;
}
else {
curState = GAME_SINGLE;
}
//store network address
}
void error(const char *msg)
{
perror(msg);
exit(1);
}
void Network::waitForConnection(int portno)
{
cout << "NET: I am a server\n===================\n";
int portnum;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
serversockfd = socket(AF_INET, SOCK_STREAM, 0);
if (serversockfd < 0)
error("ERROR opening socket");
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
if (setsockopt (serversockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
if (setsockopt (serversockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
bzero((char *) &serv_addr, sizeof(serv_addr));
portnum = portno;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
//Bind socket to server IP address?
if (bind(serversockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
//Listen for connection request
listen(serversockfd,5);
clilen = sizeof(cli_addr);
//Accept client connection request and assign new socket file descriptor
clientsockfd = accept(serversockfd, (struct sockaddr *) &cli_addr, &clilen);
if (clientsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(clientsockfd,buffer,255);
std:: string message(buffer);
if (n < 0) error("ERROR reading from socket");
//cout << "\n\n\nHere is the message: " << message << "\n\n\n";
baseapp->insertP2();
connected=true;
//close(serversockfd);
//close(clientsockfd);
}
void Network::searchForConnection(int portno, std::string networkName)
{
cout << "NET: I am a client\n===================\n";
int portnum, n;
struct sockaddr_in serv_addr;
struct hostent *server;
portnum = portno;
clientsockfd = socket(AF_INET, SOCK_STREAM, 0);
if (clientsockfd < 0)
error("ERROR opening socket");
char url[networkName.length()+1];
strcpy(url, networkName.c_str());
server = gethostbyname(url);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(clientsockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
bzero(buffer,256);
n = write(clientsockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
//n = read(clientsockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
//printf("%s\n",buffer);
//cout << "\n\n\n\n\n\n@@@@@@@@@@@@@@@@@@@@@@@ inside searchForConnection @@@@@@@@@@@@@@@@@@@@\n\n\n\n\n\n\n";
//close(clientsockfd);
baseapp->insertP2();
connected=true;
}
bool Network::sendPacket(int len)
{
int n;
if(strlen(toSendPacket)>len)
len=strlen(toSendPacket);
n = write(clientsockfd, toSendPacket, len);
if( n < 0 )
{
error("Error sending data from socket");
return false;
}
//Packet sent, zero out toSendPacket buffer
bzero(toSendPacket, 256);
//sendPacketReady = false;
return true;
}
void Network::printpacket(char * cp)
{
for(int i=0; i < 32; i++){
cout << hex << (int)((unsigned char)cp[i]) <<"_";
}
cout <<"\n";
}
bool Network::receivePacket()
{
int n;
bzero(toRecPacket,256);
n = read(clientsockfd,toRecPacket,255);
if( n < 0 )
{
error("Error reading data from socket");
return false;
}
return true;
}
bool Network::closeConnections()
{
if(this->curState ==GAME_CLIENT)
{
close(clientsockfd);
return true;
}
else if (this->curState == GAME_SERVER)
{
cout << "\n\nClosing server connection\n\n";
close(serversockfd);
close(clientsockfd);
return true;
}
else
{
return false;
}
connected=false;
}
void Network::setState(int in){
if(in==0)
curState=GAME_SINGLE;
else if(in==1)
curState=GAME_SERVER;
else if(in==2)
curState=GAME_CLIENT;
}
//setAddress function