lat_tcp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * tcp_xact.c - simple TCP transaction latency test
  3. *
  4. * Three programs in one -
  5. * server usage: tcp_xact -s
  6. * client usage: tcp_xact hostname
  7. * shutdown: tcp_xact -hostname
  8. *
  9. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  10. * additional restriction that results may published only if
  11. * (1) the benchmark is unmodified, and
  12. * (2) the version in the sccsid below is included in the report.
  13. * Support for this development by Sun Microsystems is gratefully acknowledged.
  14. */
  15. char *id = "$Id$\n";
  16. #include "bench.h"
  17. void client_main(int ac, char **av);
  18. void doserver(int sock);
  19. void doclient(int sock);
  20. void server_main(int ac, char **av);
  21. void doserver(int sock);
  22. int
  23. main(int ac, char **av)
  24. {
  25. if (ac != 2) {
  26. fprintf(stderr, "Usage: %s -s OR %s [-]serverhost\n",
  27. av[0], av[0]);
  28. exit(1);
  29. }
  30. if (!strcmp(av[1], "-s")) {
  31. if (fork() == 0) {
  32. server_main(ac, av);
  33. }
  34. exit(0);
  35. } else {
  36. client_main(ac, av);
  37. }
  38. return(0);
  39. }
  40. void
  41. client_main(int ac, char **av)
  42. {
  43. int sock;
  44. char *server;
  45. char buf[100];
  46. if (ac != 2) {
  47. fprintf(stderr, "usage: %s host\n", av[0]);
  48. exit(1);
  49. }
  50. server = av[1][0] == '-' ? &av[1][1] : av[1];
  51. sock = tcp_connect(server, TCP_XACT, SOCKOPT_NONE);
  52. /*
  53. * Stop server code.
  54. */
  55. if (av[1][0] == '-') {
  56. close(sock);
  57. exit(0);
  58. }
  59. BENCH(doclient(sock), MEDIUM);
  60. sprintf(buf, "TCP latency using %s", av[1]);
  61. micro(buf, get_n());
  62. exit(0);
  63. /* NOTREACHED */
  64. }
  65. void
  66. doclient(int sock)
  67. {
  68. char c;
  69. write(sock, &c, 1);
  70. read(sock, &c, 1);
  71. //printf("got %c",c);
  72. }
  73. void
  74. child()
  75. {
  76. wait(0);
  77. signal(SIGCHLD, child);
  78. }
  79. void
  80. server_main(int ac, char **av)
  81. {
  82. int newsock, sock;
  83. if (ac != 2) {
  84. fprintf(stderr, "usage: %s -s\n", av[0]);
  85. exit(1);
  86. }
  87. GO_AWAY;
  88. signal(SIGCHLD, child);
  89. sock = tcp_server(TCP_XACT, SOCKOPT_NONE);
  90. for (;;) {
  91. newsock = tcp_accept(sock, SOCKOPT_NONE);
  92. switch (fork()) {
  93. case -1:
  94. perror("fork");
  95. break;
  96. case 0:
  97. doserver(newsock);
  98. exit(0);
  99. default:
  100. close(newsock);
  101. break;
  102. }
  103. }
  104. /* NOTREACHED */
  105. }
  106. void
  107. doserver(int sock)
  108. {
  109. char c;
  110. int n = 0;
  111. while (read(sock, &c, 1) == 1) {
  112. write(sock, &c, 1);
  113. n++;
  114. }
  115. /*
  116. * A connection with no data means shut down.
  117. */
  118. if (n == 0) {
  119. tcp_done(TCP_XACT);
  120. kill(getppid(), SIGTERM);
  121. exit(0);
  122. }
  123. }