1 #include <stdio.h>
2 #include <time.h>
3
4 #if defined(_WIN32) || defined(WIN32)
5 #include <windows.h>
INIT(void)6 void INIT(void) {WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData);}
7 #else
8 #define INIT()
9 #include <unistd.h>
10 #include <netdb.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #endif
15
connect_to_server(const struct sockaddr_in * serv_addr)16 int connect_to_server(const struct sockaddr_in * serv_addr)
17 {
18 int sockfd;
19
20 /* Create a socket */
21 sockfd = socket(AF_INET, SOCK_STREAM, 0);
22 if (sockfd < 0) {
23 perror("ERROR opening socket");
24 return -1;
25 }
26
27 /* Connect to the server */
28 if (connect(sockfd, (const sockaddr *)serv_addr, sizeof(*serv_addr)) < 0) {
29 perror("ERROR connecting");
30 close(sockfd);
31 return -2;
32 }
33
34 return sockfd;
35 }
36
send_to_server(int conn,const char * request)37 int send_to_server(int conn, const char * request)
38 {
39 int req_len = strlen(request);
40 int n;
41
42 n = write(conn, request, req_len);
43 if (n < 0) {
44 perror("ERROR writing to socket");
45 return 0;
46 }
47
48 return (n==req_len);
49 }
50
read_from_server(int conn)51 int read_from_server(int conn)
52 {
53 char rbuffer[1024];
54 int n;
55 long ret;
56
57 n = read(conn, rbuffer, sizeof(rbuffer));
58 if (n < 0) {
59 perror("ERROR reading from socket");
60 return 0;
61 }
62
63 if (strncmp("HTTP/1.", rbuffer, 7)) {
64 perror("ERROR not a HTTP response");
65 return 0;
66 }
67
68 ret = atol(rbuffer + 9);
69
70 return ret;
71 }
72
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 long portno;
77 int i, con_count=1;
78 time_t t1,t2,t3,t4;
79 char wbuffer[256];
80 int connlist[1024*65];
81 int result[1024*65];
82 struct hostent *server;
83 struct sockaddr_in serv_addr;
84
85 INIT();
86
87 if (argc != 4) {
88 fprintf(stderr,"Usage:\n\t%s hostname port clients\n\n", argv[0]);
89 exit(0);
90 }
91
92 con_count = atol(argv[3]);
93 if (con_count<1) con_count=1;
94 if (con_count>1024*65) con_count=1024*65;
95
96 portno = atol(argv[2]);
97 if (portno<1l || portno>0xFFFFl) {
98 fprintf(stderr, "ERROR, invalid port\n");
99 exit(0);
100 }
101
102 server = gethostbyname(argv[1]);
103 if (server == NULL) {
104 fprintf(stderr, "ERROR, no such host\n");
105 exit(0);
106 }
107
108 memset(&serv_addr, 0, sizeof(serv_addr));
109 serv_addr.sin_family = AF_INET;
110 memcpy(server->h_addr, &serv_addr.sin_addr.s_addr, server->h_length);
111 serv_addr.sin_port = htons((short)portno);
112
113 sprintf(wbuffer, "GET / HTTP/1.0\r\n\r\n");
114
115 t1 = time(0);
116 for (i=0;i<con_count;i++) {
117 result[i] = connlist[i] = connect_to_server(&serv_addr);
118 }
119 t2 = time(0);
120 for (i=0;i<con_count;i++) {
121 if (result[i]>=0) {
122 result[i] = send_to_server(connlist[i], wbuffer);
123 }
124 }
125 t3 = time(0);
126 for (i=0;i<con_count;i++) {
127 if (result[i]>=0) {
128 result[i] = read_from_server(connlist[i]);
129 }
130 }
131 t4 = time(0);
132
133 printf("\n");
134 printf("conn: %.0lf\n", difftime(t2,t1));
135 printf("write: %.0lf\n", difftime(t3,t2));
136 printf("read: %.0lf\n", difftime(t4,t3));
137
138 for (i=-10;i<1000;i++) {
139 int j,cnt=0;
140 for(j=0;j<con_count;j++) {
141 if (result[j]==i) cnt++;
142 }
143 if (cnt>0) {
144 printf("%5i\t%7i\n", i, cnt);
145 }
146 }
147
148 return 0;
149 }
150
151
152