-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
244 lines (198 loc) · 6.49 KB
/
Copy pathclient.c
File metadata and controls
244 lines (198 loc) · 6.49 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
/* simple client, takes two parameters, the server domain name,
and the server port number */
typedef struct msg {
unsigned short sz;
unsigned long time1;
unsigned long time2;
char txt[65536];
} msg;
int main(int argc, char** argv) {
struct timeval tvalAfter;
gettimeofday (&tvalAfter, NULL);
/* our client socket */
int sock;
/* variables for identifying the server */
unsigned int server_addr;
struct sockaddr_in sin;
struct addrinfo *getaddrinfo_result, hints;
/* convert server domain name to IP address */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* indicates we want IPv4 */
if (getaddrinfo(argv[1], NULL, &hints, &getaddrinfo_result) == 0) {
server_addr = (unsigned int) ((struct sockaddr_in *) (getaddrinfo_result->ai_addr))->sin_addr.s_addr;
freeaddrinfo(getaddrinfo_result);
}
/* server port number */
unsigned short server_port = atoi (argv[2]);
/* size in bytes of each message to send*/
unsigned int size_param = atoi (argv[3]);
/* number of message exchanges to perform */
unsigned int count_param = atoi (argv[4]);
char *buffer, *sendbuffer;
int size = 500;
int count;
int num;
/* allocate a memory buffer in the heap */
/* putting a buffer on the stack like:
char buffer[500];
leaves the potential for
buffer overflow vulnerability */
buffer = (char *) malloc(size);
if (!buffer)
{
perror("failed to allocated buffer");
abort();
}
char input_buffer[65536];
// // first 2 bytes for the data size
// uint16_t total_size = htons((uint16_t)size_param);
// memcpy(buffer, &total_size, 2);
// // next 8 bytes for the datas time (seconds)
// uint64_t second = htons((uint16_t)tvalAfter.tv_sec);
// memcpy(buffer, &second, 8);
// // next 8 bytes for the datas time (milli-seconds)
// uint64_t milliseconds = htons((uint16_t)tvalAfter.tv_usec);
// memcpy(buffer, &milliseconds, 8);
sendbuffer = (char *) malloc(size);
if (!sendbuffer)
{
perror("failed to allocated sendbuffer");
abort();
}
/* create a socket */
if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror ("opening TCP socket");
abort ();
}
/* fill in the server's address */
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = server_addr;
sin.sin_port = htons(server_port);
/* connect to the server */
if (connect(sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)
{
perror("connect to server failed");
abort();
}
scanf("%s", input_buffer);
// create the struct for the msg
msg curr_msg;
curr_msg.sz = size_param;
curr_msg.time1 = 0;
curr_msg.time2 = 0;
strcpy(curr_msg.txt, input_buffer);
// Quick sanity check
printf("Input txt is: \n");
printf(curr_msg.txt);
printf("\n");
// Now that we've successfully connected, let's send a ping to the server.
for (unsigned int i = 0; i < count_param; i++) {
// int ping_send = send(sock, input_buffer, size_param, 0);
// int ping_send = send(sock, "PING", 5, 0);
// Time this message will be sent
curr_msg.time1 = tvalAfter.tv_sec;
curr_msg.time2 = tvalAfter.tv_usec;
// NOTE: need to do network-byte order stuff here
int ping_send = send(sock, &curr_msg, size_param, 0);
if (ping_send < 0) {
printf("Error with sending.\n");
break;
}
printf("Successfully sent from client...Count = %u\n", i +1 );
printf("Time = %u\n", curr_msg.time2);
// Now let's receive the pong back.
int pong_receive = recv(sock, buffer, size_param, 0);
// int pong_receive = recv(sock, buffer, 5, 0);
if (pong_receive < 0) {
printf("Error with receiving.\n");
break;
}
printf(buffer);
printf("\n");
// return 0;
}
printf("got here client\n");
// /* everything looks good, since we are expecting a
// message from the server in this example, let's try receiving a
// message from the socket. this call will block until some data
// has been received */
// count = recv(sock, buffer, size, 0);
// if (count < 0)
// {
// perror("receive failure");
// abort();
// }
// /* in this simple example, the message is a string,
// we expect the last byte of the string to be 0, i.e. end of string */
// if (buffer[count-1] != 0)
// {
// /* In general, TCP recv can return any number of bytes, not
// necessarily forming a complete message, so you need to
// parse the input to see if a complete message has been received.
// if not, more calls to recv is needed to get a complete message.
// */
// printf("Message incomplete, something is still being transmitted\n");
// }
// else
// {
// // printf("Here is what we got: %s", buffer);
// printf(buffer);
// }
// while (1){
// printf("\nEnter the type of the number to send (options are char, short, int, or bye to quit): ");
// fgets(buffer, size, stdin);
// if (strncmp(buffer, "bye", 3) == 0) {
// /* free the resources, generally important! */
// close(sock);
// free(buffer);
// free(sendbuffer);
// return 0;
// }
// /* first byte of the sendbuffer is used to describe the number of
// bytes used to encode a number, the number value follows the first
// byte */
// if (strncmp(buffer, "char", 4) == 0) {
// sendbuffer[0] = 1;
// } else if (strncmp(buffer, "short", 5) == 0) {
// sendbuffer[0] = 2;
// } else if (strncmp(buffer, "int", 3) == 0) {
// sendbuffer[0] = 4;
// } else {
// printf("Invalid number type entered, %s\n", buffer);
// continue;
// }
// printf("Enter the value of the number to send: ");
// fgets(buffer, size, stdin);
// num = atol(buffer);
// switch(sendbuffer[0]) {
// case 1:
// *(char *) (sendbuffer+1) = (char) num;
// break;
// case 2:
// /* for 16 bit integer type, byte ordering matters */
// *(short *) (sendbuffer+1) = (short) htons(num);
// break;
// case 4:
// /* for 32 bit integer type, byte ordering matters */
// *(int *) (sendbuffer+1) = (int) htonl(num);
// break;
// default:
// break;
// }
// // send(sock, sendbuffer, sendbuffer[0]+1, 0);
// send(sock, "PING", 5, 0);
// }
return 0;
}