-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
72 lines (66 loc) · 1.41 KB
/
helpers.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#define CHUNK_SIZE_BYTES 1024
#define BASE_PORT 8080
#define IP "127.0.0.1"
//recives an integer over the socket and returns
//copied from stack overflow
int receive_int(int *num, int fd)
{
int32_t ret;
char *data = (char*)&ret;
int left = sizeof(ret);
int rc;
do {
rc = read(fd, data, left);
if (rc <= 0) {
perror("[-] Error writing int to socket");
return -1;
}
else {
data += rc;
left -= rc;
}
}
while (left > 0);
*num = ntohl(ret);
return 0;
}
//sends an integer over the socket to the receiver
//copied from stackoverflow
int send_int(int num, int fd)
{
int32_t conv = htonl(num);
char *data = (char*)&conv;
int left = sizeof(conv);
int rc;
do {
rc = write(fd, data, left);
if (rc < 0) {
perror("[-] Error writing int to socket");
return -1;
}
else {
data += rc;
left -= rc;
}
}
while (left > 0);
return 0;
}
//calculates the file size in bytes for given file pointer
int file_size_calculate(FILE *fp)
{
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
return size;
}
//port for ith thread
int port_for_ith_thread(int i)
{
return BASE_PORT+2*i;
}