-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
144 lines (121 loc) · 3.24 KB
/
client.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
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/tcp.h>
#include <unistd.h>
#include "helpers.h"
int main(int argc, char **argv) {
const char* server_name = "141.212.110.206";
int server_port = 8877;
int num_times = 1;
int wait_time = 3;
bool do_forever = false;
// data that will be sent to the server
const char* data_to_send = "Gangadhar Hi Shaktimaan hai";
if (argc <= 1) {
printf("Usage: ./client [num_times] [server_name] [server_port] [wait_time] [data_to_send]\n");
}
if (argc > 1) {
num_times = atoi(argv[1]);
if (num_times == -1)
do_forever = true;
}
if (argc > 2) {
server_name = argv[2];
}
if (argc > 3) {
server_port = atoi(argv[3]);
}
if (argc > 4) {
wait_time = atoi(argv[4]);
}
if (argc > 5) {
data_to_send = argv[5];
}
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
// creates binary representation of server name
// and stores it as sin_addr
// http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html
inet_pton(AF_INET, server_name, &server_address.sin_addr);
// htons: port in network order format
server_address.sin_port = htons(server_port);
// open a stream socket
int sock;
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}
// check TCP fastopen options (im just curious)
if (check_tcp_fastopen(sock)) {
printf("could not check tcp fastopen options on client socket");
return 1;
}
// if migration not enabled on client sock, enable it
bool enabled;
if (is_migrate_enabled(sock, &enabled)) {
puts("could not check migrate_enabled on client");
return 1;
}
if (!enabled && set_migrate_enabled(sock, true)) {
puts("could not enable migration on client");
return 1;
}
// set the migration token of the client
int token = 1234567;
/*
if (setsockopt(sock, SOL_SOCKET, TCP_MIGRATE_ENABLED, &token, sizeof(uint32_t)) < 0) {
printf("setsockopt(SO_REUSEADDR) failed");
return -1;
}
*/
if (set_migrate_token(sock, token)) {
puts("could not set migrate token of client");
return 1;
}
// TCP is connection oriented, a reliable connection
// **must** be established before any data is exchanged
if (connect(sock, (struct sockaddr*)&server_address,
sizeof(server_address)) < 0) {
printf("could not connect to server\n");
return 1;
}
if (close_on_kill(sock)) {
perror("could not set up inthandler");
return 1;
}
char buffer[1000];
for (int i = 0; i < num_times || do_forever; i++) {
if (i != 0) {
sleep(wait_time);
}
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "[%i] %s", i, data_to_send);
// send
send(sock, buffer, strlen(buffer), 0);
// receive
int n = 0;
int len = 0, maxlen = 100;
char buffer[maxlen];
memset(buffer, 0, sizeof(buffer));
char* pbuffer = buffer;
// will remain open until the server echos back the content
recv(sock, pbuffer, maxlen, 0);
printf("received: '%s'\n", buffer);
/*
while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
pbuffer += n;
maxlen -= n;
len += n;
buffer[len] = '\0';
printf("received: '%s'\n", buffer);
}
*/
}
// close the socket
close(sock);
return 0;
}