testclient.c 3.2 KB

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