-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
56 lines (42 loc) · 1.06 KB
/
common.cpp
File metadata and controls
56 lines (42 loc) · 1.06 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
#include <bits/stdc++.h>
#include "common.h"
#include <sys/socket.h>
#include <sys/types.h>
// receive len bytes from sockfd to buffer
int recv_all(int sockfd, void *buffer, size_t len) {
size_t bytes_received = 0;
size_t bytes_remaining = len;
char *buff = (char *)buffer;
while(bytes_remaining) {
size_t n_bytes = recv(sockfd, buff + bytes_received,
bytes_remaining, 0);
if(n_bytes <= 0)
return n_bytes;
bytes_received += n_bytes;
bytes_remaining -= n_bytes;
}
return bytes_received;
}
// send len bytes from buffer to sockfd
int send_all(int sockfd, void *buffer, size_t len) {
size_t bytes_sent = 0;
size_t bytes_remaining = len;
char *buff = (char *)buffer;
while(bytes_remaining) {
size_t n_bytes = send(sockfd, buff + bytes_sent,
bytes_remaining, 0);
if(n_bytes <= 0)
return n_bytes;
bytes_sent += n_bytes;
bytes_remaining -= n_bytes;
}
return bytes_sent;
}
int get_content_len(char *content) {
for (int i = 0; i < LEN_CONTENT; i++) {
if (content[i] == '\0') {
return i + 1;
}
}
return LEN_CONTENT;
}