-
Notifications
You must be signed in to change notification settings - Fork 0
/
communications.h
116 lines (100 loc) · 2.78 KB
/
communications.h
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
/*
* communications.h
* PA03 project
* CSE 30264 - Computer Networks - Fall 2019
* bblum1, cfoley, cmarkley
*
* This file holds functions to allow simple communication over a TCP connection.
*/
/*
* Sends `size` amount of `buffer` via TCP connection to `clientSocket`
*/
int send_buffer(int clientSocket, char *buffer, int size) {
int len;
if ((len = write(clientSocket, buffer, size)) == -1) {
perror("Client Send\n");
exit(1);
}
return len;
}
/*
* Listens on the TCP connection on `clientSocket`.
* Reads in `size` amount of data into `buffer`
*/
int receive_buffer(int clientSocket, char * buffer, int size) {
int len;
bzero(buffer, sizeof(buffer));
if ((len = read(clientSocket, buffer, size)) == -1) {
perror("Client Receive!");
exit(1);
}
return len;
}
/*
* sends an int to clientSocket via TCP connection
*/
int send_int(int clientSocket, int value) {
int len;
uint32_t temp = htonl(value);
if ((len = write(clientSocket, &temp, sizeof(uint32_t))) == -1) {
perror("Client Send");
exit(1);
}
return len;
}
/*
* Listens for an int on a TCP connection with clientSocket
*/
int receive_int(int clientSocket) {
int buffer;
int len;
bzero(&buffer, sizeof(buffer));
if ((len = read(clientSocket, &buffer, sizeof(uint32_t))) == -1) {
perror("Client Receive Error");
exit(1);
}
int temp = ntohl(buffer);
return temp;
}
/*
* Abstraction of send_buffer and send_int to allow a message to send to clientSocket
* with automatic sizing. It sends the number of characters within the message
* (excluding the null chracter). It then sends those exact characters over a
* TCP connection to `clientSocket`.
* Complement of receive_string().
*/
void send_string(int clientSocket, char * message) {
// Send the number of characters (NOT INCLUDING NULL CHARACTER)
if(send_int(clientSocket, strlen(message)) < 0) {
debug("Failed to send int");
// TODO: exit here
};
// Send those characters
if(send_buffer(clientSocket, message, strlen(message)) < 0) {
debug("Failed to send buffer\n");
// TODO: exit here
};
}
/*
* Abstraction of receive_buffer and receive_int to allow receiving of a message
* sent via send_string().
* Listens for the size of the message.
* Declares a buffer of [size+1] to make room for the null
* Receives the characters
* Sets buffer[size] to null character.
*/
char * receive_string(int clientSocket) {
// Listen for the size
int size = receive_int(clientSocket);
// Allocate buffer
char * buffer = malloc(sizeof(char)*(size + 1)); // TODO enter some fucking frees()
bzero(buffer, sizeof(buffer));
// Receive message
if(receive_buffer(clientSocket, buffer, size) < 0){
debug("Failed to receive buffer");
// TODO: exit here
}
// Null terminate string
buffer[size] = '\0';
return buffer;
}