-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.cpp
More file actions
38 lines (34 loc) · 1.03 KB
/
Copy pathoperations.cpp
File metadata and controls
38 lines (34 loc) · 1.03 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
#include "utils.h"
int send_all(int socket, void *buffer, size_t length) {
char *buf = (char *) buffer;
size_t bytes_sent = 0;
while (bytes_sent < length) {
ssize_t result = send(socket, buf + bytes_sent, length - bytes_sent, 0);
if (result < 0) {
perror("Error at send");
return -1;
}
bytes_sent = bytes_sent + result;
}
return bytes_sent;
}
int recv_all(int socket, void *buffer, size_t length) {
char *buf = (char *) buffer;
size_t bytes_received = 0;
while (bytes_received < length) {
size_t result = recv(socket, buf + bytes_received, length - bytes_received, 0);
if (result < 0) {
perror("Error at recv");
return result;
}
if (result == 0) {
break;
}
bytes_received = bytes_received + result;
}
return bytes_received;
}
void send_message(int socket, void *buffer, size_t length) {
send_all(socket, &length, sizeof(length));
send_all(socket, buffer, length);
}