-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySocket.cpp
More file actions
370 lines (308 loc) · 8.39 KB
/
MySocket.cpp
File metadata and controls
370 lines (308 loc) · 8.39 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//BTN415 - MS3
//Ehsan Ekbatani, Sina Lahsaee, Cheng-Tuo Shueh
#include <string>
#include "MySocket.h"
//Object constructor taking in socket type (TCP/UDP), IP, port, connection type (Server/Client), and buffer size
MySocket::MySocket(SocketType sock, std::string IP, unsigned int port, ConnectionType connection, unsigned int size) {
//Initialize sockets to safe empty
WelcomeSocket = INVALID_SOCKET;
ConnectionSocket = INVALID_SOCKET;
bConnect = false;
//Assign properties
SetType(sock);
SetIPAddr(IP);
SetPortNum(port);
connectionType = connection;
//Allocate size of buffer, if size is invalid, set to default
if (size <= 0) {
MaxSize = DEFAULT_SIZE;
}
else {
MaxSize = size;
}
Buffer = new char[MaxSize];
//Initialize the winsock DLL, proceed only if successful
if (!StartWSA()) {
std::cout << "Could not start DLLs" << std::endl;
delete[] Buffer;
Buffer = nullptr;
}
}
//Object destructor
MySocket::~MySocket() {
//Close all sockets
closesocket(ConnectionSocket);
ConnectionSocket = INVALID_SOCKET;
closesocket(WelcomeSocket);
WelcomeSocket = INVALID_SOCKET;
WSACleanup();
bConnect = false;
//De-allocate dynamic memory
delete[] Buffer;
Buffer = nullptr;
}
/* SOCKET INIT FUNCTIONS */
//Start DLL and return result
bool MySocket:: StartWSA() {
//WSAStartup() returns 0 if it's successful
return WSAStartup(MAKEWORD(2, 2), &wsa_data) == 0;
}
//Set up TCP socket, return results
bool MySocket::ConnectTCP() {
if (connectionType != TCP) {
//Prevent non-TCP setups
return false;
}
else {
//This will be the welcome socket for TCP server, and the connection socket for TCP client (so we can init just once for both cases)
WelcomeSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//Proceed only if successful
if (WelcomeSocket == INVALID_SOCKET) {
std::cout << "Could not initialize socket" << std::endl;
delete[] Buffer;
Buffer = nullptr;
return false;
}
else {
//Set the target address
SvrAddr.sin_family = AF_INET;
SvrAddr.sin_port = htons(Port);
SvrAddr.sin_addr.s_addr = inet_addr(IPAddr.c_str());
//Additional setup depending on connection type
if (mySocket == SERVER) {
//Bind TCP socket, proceeding only if successful
if ((bind(this->WelcomeSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr))) == SOCKET_ERROR) {
//Close welcome socket upon failure
closesocket(this->WelcomeSocket);
WelcomeSocket = INVALID_SOCKET;
std::cout << "Could not bind to the TCP socket" << std::endl;
delete[] Buffer;
Buffer = nullptr;
return false;
}
else {
//Set TCP socket to listen mode, proceed only if successful
if (listen(WelcomeSocket, 1) == SOCKET_ERROR) {
closesocket(WelcomeSocket);
WelcomeSocket = INVALID_SOCKET;
std::cout << "Could not listen to the provided TCP socket." << std::endl;
delete[] Buffer;
Buffer = nullptr;
return false;
}
else {
//Accept incoming connections
std::cout << "Waiting for TCP client connection" << std::endl;
if ((ConnectionSocket = accept(this->WelcomeSocket, NULL, NULL)) == SOCKET_ERROR) {
closesocket(this->WelcomeSocket);
WelcomeSocket = INVALID_SOCKET;
ConnectionSocket = INVALID_SOCKET;
std::cout << "Could not accept incoming TCP connection." << std::endl;
delete[] Buffer;
Buffer = nullptr;
return false;
}
else {
//Return status of connection attempt
bConnect = true;
return bConnect;
}
}
}
}
else if (mySocket == CLIENT) {
//Attempt to connect to server and return status
if ((connect(this->WelcomeSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr))) == SOCKET_ERROR) {
closesocket(this->WelcomeSocket);
WelcomeSocket = INVALID_SOCKET;
std::cout << "Could not connect to the TCP server" << std::endl;
return false;
}
else {
bConnect = true;
return bConnect;
}
}
else {
return false;
}
}
}
}
//Attempt to disconnect the client-server connection
bool MySocket::DisconnectTCP() {
if (connectionType != TCP) {
//Prevent non-TCP setups
return false;
}
else {
//Close all sockets
bool succ = true;
if (mySocket == SERVER) {
succ = closesocket(ConnectionSocket) != SOCKET_ERROR;
}
succ = closesocket(WelcomeSocket) != SOCKET_ERROR;
if (succ) {
ConnectionSocket = INVALID_SOCKET;
WelcomeSocket = INVALID_SOCKET;
bConnect = false;
}
return succ;
}
}
//Set up UDP socket, return results
bool MySocket::SetupUDP() {
if (connectionType != UDP) {
//Prevent non-UDP setups
return false;
}
else {
ConnectionSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//Proceed only if successful
if (ConnectionSocket == INVALID_SOCKET) {
std::cout << "Could not initialize socket" << std::endl;
delete[] Buffer;
Buffer = nullptr;
return false;
}
else {
if (mySocket == SERVER) {
//Allow any address
SvrAddr.sin_family = AF_INET;
SvrAddr.sin_port = htons(Port);
SvrAddr.sin_addr.s_addr = INADDR_ANY;
//Bind UDP server
if (bind(ConnectionSocket, (struct sockaddr*)&SvrAddr, sizeof(SvrAddr)) == SOCKET_ERROR) {
closesocket(ConnectionSocket);
;
std::cout << "Could not bind to the UDP socket" << std::endl;
return false;
}
else {
std::cout << "Waiting for UDP datagrams" << std::endl;
bConnect = true;
return bConnect;
}
}
else if (mySocket == CLIENT) {
//No further action required for UDP client
bConnect = true;
return bConnect;
}
else {
return false;
}
}
}
}
//Close down UDP socket
bool MySocket::TerminateUDP() {
if (connectionType != UDP) {
//Prevent non-UDP setups
return false;
}
else {
bool ret = closesocket(ConnectionSocket) != SOCKET_ERROR;
//Set connection status on successful termination
if (ret) {
ConnectionSocket = INVALID_SOCKET;
bConnect = false;
}
return ret;
}
}
/* DATA TRANSFER FUNCTIONS */
//All-inclusive send data over socket
int MySocket::SendData(const char* data, int size) {
unsigned int rx = 0;
//Send data differently depending on socket type
if (connectionType == TCP) {
if (mySocket == SERVER) {
rx = send(ConnectionSocket, data, size, 0);
}
else if (mySocket == CLIENT) {
rx = send(WelcomeSocket, data, size, 0);
}
}
else if (connectionType == UDP) {
if (mySocket == SERVER) {
rx = sendto(ConnectionSocket, data, size, 0, (struct sockaddr*)&RespAddr, sizeof(RespAddr));
}
else if (mySocket == CLIENT) {
SvrAddr.sin_family = AF_INET;
SvrAddr.sin_port = htons(Port);
SvrAddr.sin_addr.s_addr = inet_addr(IPAddr.c_str());
rx = sendto(ConnectionSocket, data, size, 0, (struct sockaddr*)&SvrAddr, sizeof(SvrAddr));
}
}
return rx;
}
//All-inclusive get data from socket
int MySocket::GetData(char* data) {
unsigned int rx = 0;
//Clear the buffer before storing more into it (just in case there was something there before)
memset(Buffer, 0, MaxSize);
//Receive data differently depending on socket type
if (connectionType == TCP) {
if (mySocket == SERVER) {
rx = recv(ConnectionSocket, Buffer, MaxSize, 0);
}
else if (mySocket == CLIENT) {
rx = recv(WelcomeSocket, Buffer, MaxSize, 0);
}
//User must ensure data has enough space to copy to
memcpy(data, Buffer, rx);
}
else if (connectionType == UDP) {
int respLen = sizeof(RespAddr);
rx = recvfrom(ConnectionSocket, Buffer, MaxSize, 0, (struct sockaddr *)&RespAddr, &respLen);
//User must ensure data has enough space to copy to
memcpy(data, Buffer, rx);
}
return rx;
}
/* GETTERS AND SETTERS */
//Get IP Address
std::string MySocket::GetIPAddr() {
return this->IPAddr;
}
//Set IP Address after ensuring socket isn't active
bool MySocket::SetIPAddr(std::string IP) {
//Only set if there is no active socket(s)
if (bConnect) {
return false;
}
else {
IPAddr = IP;
return true;
}
}
//Get Port number
int MySocket::GetPort() {
return this->Port;
}
//Set Port number after ensuring socket isn't active
bool MySocket::SetPortNum(int portNum) {
//Only set if there is no active socket(s)
if (bConnect) {
return false;
}
else {
Port = portNum;
return true;
}
}
//Get the socket type (Client/Server)
SocketType MySocket::GetType() {
return this->mySocket;
}
//Set the socket type (Client/Server) after ensuring socket isn't active
bool MySocket::SetType(SocketType socket) {
if (bConnect) {
return false;
}
else {
mySocket = socket;
return true;
}
}