-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recive.hpp
279 lines (183 loc) · 5.53 KB
/
Recive.hpp
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
#ifdef _WIN32
# include <winsock2.h>
# include <Ws2tcpip.h>
# include <windows.h>
# pragma comment(lib, "Ws2_32.lib")
# define WIN TRUE
#else
#include <sys/sockets.h>
#endif
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <typeinfo>
#include <vector>
#include <list>
#include <sstream>
#include <iomanip>
#include <tchar.h>
using namespace std;
// Zmienne globalne
#define BUFF 1024
#define BUFFID 3
class ReciveSockets {
private:
int InitializeWinSock() {
WSADATA wsaData;
int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (res != 0) {
printf("// WSAStartup failed with error %d\n", res);
return 1;
}
printf("// Winsock Initialized\n");
return 0;
}
//Initializng sockets
int check_if_socket_valid() {
if (SendSock != INVALID_SOCKET && RecvSock != INVALID_SOCKET) {
printf("// Sockets succesfully initialized\n");
return 0;
}
printf("// Sockets initialization failed %d\n", WSAGetLastError());
return 1;
}
int InitializeSocket() {
SendSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
RecvSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
return check_if_socket_valid();
}
void adress_sockets(int port, string host) {
char* char_arr;
char_arr = &host[0];
SendSockAddr.sin_family = AF_INET;
SendSockAddr.sin_port = htons(port + 1);
RecvSockAddr.sin_family = AF_INET;
RecvSockAddr.sin_port = htons(port);
SendSockAddr.sin_addr.s_addr = inet_addr(char_arr);
RecvSockAddr.sin_addr.s_addr = inet_addr(char_arr);
}
int attempt_biniding_recive_sock() {
if (bind(RecvSock, (SOCKADDR*)&RecvSockAddr, sizeof(RecvSockAddr))) {
printf("// bind failed with error %d\n", WSAGetLastError());
return 1;
}
printf("// Socket succesfully binded\n");
return 0;
}
//build sockets
int BuildSocket(int port, string host) {
adress_sockets(port, host);
return attempt_biniding_recive_sock();
}
public:
SOCKET SendSock = INVALID_SOCKET;
SOCKET RecvSock = INVALID_SOCKET;
struct sockaddr_in SendSockAddr;
struct sockaddr_in RecvSockAddr;
int close_socket() {
closesocket(SendSock);
closesocket(RecvSock);
WSACleanup();
return 1;
}
int initalize(int port, string host) {
if (WIN) {
InitializeWinSock();
}
if (InitializeSocket() || BuildSocket(port, host)) {
printf("aborting process due to above error");
return 1;
}
return 0;
}
};
//COMPILE COMMAND
//g++ -o prog.exe Recive.cpp -lws2_32 -pthread
class Reciveconnnection{
private:
ReciveSockets s;
char SendMsgBuffered[BUFF - 4];
//RECIVING
string ReciveFunc() {
char RecvBufferedMsg[BUFF];
int ServerAddrSize = sizeof(s.RecvSockAddr);
int recived = recvfrom(s.RecvSock, RecvBufferedMsg, 1024, 0 , (SOCKADDR *)& s.RecvSockAddr, &ServerAddrSize);
if (recived == SOCKET_ERROR) {
printf("// recvfrom failed with error %d\n", WSAGetLastError());
return "1";
}
RecvBufferedMsg[recived] = '\0';
string x;
for (int i = 0; i < recived; i++) {
x.push_back(RecvBufferedMsg[i]);
}
return x;
}
//SEND
int SendFunc(std::string x){
for (int i =0; i<x.length(); i++) {
SendMsgBuffered[i] = x[i];
}
int MsgLen = (int)(x.length());
int result = sendto(s.SendSock, SendMsgBuffered, MsgLen, 0 , (SOCKADDR *) & s.SendSockAddr, sizeof(s.SendSockAddr));
if (result == SOCKET_ERROR) {
printf("// Got an error when sending message back: %d\n", WSAGetLastError());
return 1;
}
return 0;
}
string ReciveWithoutInit(){
int Recived = 0;
string msg;
int FinMessage[1000] = {0};
int ID;
int LastID = -2;
int size = 0;
string ans[1000] = { "" };
while (1 == 1) {
Recived = 0;
msg = ReciveFunc();
if (msg.substr(1, 3) == "000") {
ID = 0;
ans[ID] = msg.substr(3, msg.size());
}
else {
std::stringstream str(msg.substr(0, 3));
str >> ID;
ans[ID] = msg.substr(3, msg.size());
}
SendFunc(to_string(ID));
FinMessage[ID] = msg.size();
if (FinMessage[0] > msg.size()) {
LastID = ID;
cout << "LAST ID RECIVED\n";
}
if (LastID != -1) {
for (int i = 0; i <= LastID; i++) {
if (FinMessage[i])
Recived++;
}
}
if (Recived == LastID + 1) {
cout << "END" << endl;
break;
}
}
string finalans = "";
for (int i = 0; i < LastID + 1; i++) {
finalans += ans[i];
}
return finalans;
}
public:
Reciveconnnection(string host, int port){
s.initalize(port, host);
}
string Recive() {
return ReciveWithoutInit();
}
};
string Recive(string host, int port){
Reciveconnnection connection = Reciveconnnection(host,port);
return connection.Recive();
}