-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
64 lines (55 loc) · 1.28 KB
/
client.c
File metadata and controls
64 lines (55 loc) · 1.28 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
// Source: https://book.systemsapproach.org/foundation/software.html
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define SERVER_PORT 5432
int main(int argc, char * argv[])
{
struct hostent *hp;
struct sockaddr_in sin;
char *host;
unsigned char buf[64];
int s;
int requests = 100000;
int i;
if (argc != 2) {
printf("Usage: <ip-address-of-server>\n");
exit(0);
}
host = argv[1];
/* translate host name into peer's IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
exit(1);
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("connect");
close(s);
exit(1);
}
for (i = 0; i < requests; i++) {
memset(buf, 0x7f, sizeof(buf));
if (send(s, buf, sizeof(buf), 0) != sizeof(buf)) {
perror("send failed\n");
}
}
close(s);
return 0;
}