-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
42 lines (38 loc) · 930 Bytes
/
common.c
File metadata and controls
42 lines (38 loc) · 930 Bytes
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
#include "common.h"
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
int recv_all(int sockfd, void *buffer, size_t len) {
size_t bytes_received = 0;
size_t bytes_remaining = len;
char *buff = buffer;
while(bytes_remaining != 0) {
ssize_t received = recv(sockfd, buff + bytes_received, bytes_remaining, 0);
if(received <= 0) {
break;
}
bytes_remaining -= received;
bytes_received += received;
}
/*
Returnam exact cati octeti am citit
*/
return bytes_received;
}
int send_all(int sockfd, void *buffer, size_t len) {
size_t bytes_sent = 0;
size_t bytes_remaining = len;
char *buff = buffer;
while(bytes_remaining) {
ssize_t sent = send(sockfd, buff + bytes_sent, bytes_remaining, 0);
if(sent <= 0) {
break;
}
bytes_remaining -= sent;
bytes_sent += sent;
}
/*
Returnam exact cati octeti am trimis
*/
return bytes_sent;
}