-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCP_Sender.c
169 lines (145 loc) · 4.36 KB
/
TCP_Sender.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#define BUFFER_SIZE (2*1024*1024)
/*
* @brief
A random data generator function based on srand() and rand().
* @param size
The size of the data to generate (up to 2^32 bytes).
* @return
A pointer to the buffer.
*/
char *util_generate_random_data(unsigned int size)
{
char *buffer = NULL;
// Argument check.
if (size == 0)
return NULL;
buffer = (char *)calloc(size, sizeof(char));
// Error checking.
if (buffer == NULL)
return NULL;
// Randomize the seed of the random number generator.
srand(time(NULL));
for (unsigned int i = 0; i < size; i++) {
*(buffer + i) = ((unsigned int)rand() % 256);
// 'F' and 'E' reserved for Finish and End flags respectively
if ((*(buffer + i) == 'F') || (*(buffer + i) == 'E'))
i--;
}
return buffer;
}
int main(int argc, char *argv[])
{
char buffer[BUFFER_SIZE] = {0};
// Generate some random data.
char *message = util_generate_random_data(BUFFER_SIZE);
if (message == NULL)
{
perror("util_generate_random_data() failed");
return -1;
}
printf("Generated %d bytes of random data\n", BUFFER_SIZE);
// Create a socket.
int sock = -1;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("socket(2) failed");
return -1;
}
if (strcmp(argv[6], "reno") == 0)
{
printf("Setting TCP to Reno\n");
if (setsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, "reno", strlen("reno")) != 0)
{
perror("setsockopt() failed");
return -1;
}
}
else if (strcmp(argv[6], "cubic") == 0)
{
printf("Setting TCP to Cubic\n");
if (setsockopt(sock, IPPROTO_TCP, TCP_CONGESTION, "cubic", strlen("cubic")) != 0)
{
perror("setsockopt() failed");
return -1;
}
}
else
{
printf("Invalid TCP congestion control algorithm\n");
return -1;
}
// Create a server address.
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
// Set the server address.
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(atoi(argv[4]));
// Connect to the server.
if (connect(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
{
perror("connect() failed");
close(sock);
return -1;
}
printf("connected to server\n");
char again = 'y';
while (again == 'y')
{
// Send the data.
int bytesSent = 0;
while (bytesSent < BUFFER_SIZE)
{
int ret = send(sock, message + bytesSent, BUFFER_SIZE - bytesSent, 0);
if (ret < 0)
{
perror("send(2)");
close(sock);
return 1;
}
bytesSent += ret;
printf("Sent %d bytes\n", bytesSent);
}
printf("Sending Finish message to the server\n");
char *finishMessage = "F";
send(sock, finishMessage, strlen(finishMessage), 0);
printf("Finish message sent\n");
printf("Do you want to send the message again? (y/n): ");
scanf(" %c", &again);
}
// Send exit message to the server
printf("Sending exit message to the server\n");
char *exitMessage = "E";
send(sock, exitMessage, strlen(exitMessage), 0);
printf("Exit message sent\n");
// Receive a message from the server.
int bytes_received = recv(sock, buffer, sizeof(buffer), 0);
if (bytes_received <= 0)
{
perror("recv(2)");
close(sock);
return 1;
}
// Ensure that the buffer is null-terminated, no matter what message was received.
// This is important to avoid SEGFAULTs when printing the buffer.
if (buffer[BUFFER_SIZE - 1] != '\0')
buffer[BUFFER_SIZE - 1] = '\0';
// Print the received message.
// fprintf(stdout, "Got %d bytes from the server, which says: %s\n", bytes_received, buffer);
// Close the socket with the server.
close(sock);
fprintf(stdout, "Connection closed!\n");
free(message);
// Return 0 to indicate that the client ran successfully.
return 0;
}