-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketFunctions.cpp
143 lines (104 loc) · 3.8 KB
/
SocketFunctions.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
#include "SocketFunctions.h"
string readString(int socketFd, int bufferSize){
int strSize;
if (recv(socketFd, &strSize, sizeof(int), MSG_WAITALL) == -1){
perror("Couldnt read strSize");
exit(EXIT_FAILURE);
}
char temp[strSize];
memset(temp, '\0', sizeof(strSize));
int counter = 0;
for (int i = 0; i < strSize/bufferSize; i++){
if (recv(socketFd, &(temp[counter]), bufferSize, MSG_WAITALL) == -1){
perror("Couldnt read string");
exit(EXIT_FAILURE);
}
counter += bufferSize;
}
if (counter < strSize){ /* write the remaining bytes */
int tempBuff = strSize - counter; /* tempBuff will always be smaller that the bufferSize */
if (recv(socketFd , &(temp[counter]),tempBuff, MSG_WAITALL) == -1){
perror("Couldnt read string");
exit(EXIT_FAILURE);
}
}
temp[strSize] = '\0';
string retStr(temp);
return retStr;
}
int readInt(int socketFd){
int temp;
if (recv(socketFd, &temp, sizeof(int), MSG_WAITALL) == -1){
perror("Couldnt read integer");
exit(EXIT_FAILURE);
}
return temp;
}
int sendString(int socketFd, const char* strToSend, int bufferSize){
int strSize = strlen(strToSend);
if (send(socketFd , &strSize , sizeof(int), 0) == -1){
perror("Couldnt write strSize");
exit(EXIT_FAILURE);
}
int counter = 0;
for (int i = 0; i < strSize/bufferSize; i++){
if (send(socketFd , &(strToSend[counter]) , bufferSize, 0 ) == -1){
perror("Couldnt write strToSend");
exit(EXIT_FAILURE);
}
counter += bufferSize;
}
if (counter < strSize){ /* write the remaining bytes */
int tempBuff = strSize - counter; /* tempBuff will always be smaller that the bufferSize */
if (send(socketFd , &(strToSend[counter]) , tempBuff, 0) == -1){
perror("Couldnt write strToSend");
exit(EXIT_FAILURE);
}
}
return 1;
}
void sendInt(int socketFd, int x){
int temp = x;
if (send(socketFd, &temp, sizeof(int), 0) == -1){
perror("Couldnt write integer");
exit(EXIT_FAILURE);
}
}
char* readBloom(int socketFd, int bloomSize, int bufferSize){
char* temp = new char[bloomSize + 1];
memset(temp,'\0', bloomSize + 1);
int counter = 0;
for (int i = 0; i < bloomSize/bufferSize; i++){
if (recv(socketFd, &(temp[counter]), bufferSize, MSG_WAITALL) == -1){
perror("Couldnt read bloomSize");
exit(EXIT_FAILURE);
}
counter += bufferSize;
}
if (counter < bloomSize){ /* read the remaining bytes */
int tempBuff = bloomSize - counter; /* tempBuff will always be smaller that the bufferSize */
if (recv(socketFd, &(temp[counter]), tempBuff, MSG_WAITALL) == -1){
perror("Couldnt read bloom");
exit(EXIT_FAILURE);
}
}
temp[bloomSize + 1] = '\0';
return temp;
}
void sendBloom(int socketFd, const char* strToSend, int bloomSize, int bufferSize){
int counter = 0;
for (int i = 0; i < bloomSize/bufferSize; i++){
if (send(socketFd, &(strToSend[counter]), bufferSize, 0) == -1){
perror("Couldnt write bloomSize");
exit(EXIT_FAILURE);
}
counter += bufferSize;
}
if (counter < bloomSize){ /* write the remaining bytes */
int tempBuff = bloomSize - counter; /* tempBuff will always be smaller that the bufferSize */
if (send(socketFd, &(strToSend[counter]), tempBuff, 0) == -1){
perror("Couldnt write bloom");
exit(EXIT_FAILURE);
}
}
}