clientrst.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "unp.h"
  2. #define MAXN 16384 /* max #bytes to request from server */
  3. int
  4. main(int argc, char **argv)
  5. {
  6. int i, j, fd, nchildren, nloops, nbytes;
  7. pid_t pid;
  8. ssize_t n;
  9. char request[MAXLINE], reply[MAXN];
  10. if (argc != 6)
  11. err_quit("usage: client <hostname or IPaddr> <port> <#children> "
  12. "<#loops/child> <#bytes/request>");
  13. nchildren = atoi(argv[3]);
  14. nloops = atoi(argv[4]);
  15. nbytes = atoi(argv[5]);
  16. snprintf(request, sizeof(request), "%d\n", nbytes); /* newline at end */
  17. for (i = 0; i < nchildren; i++) {
  18. if ( (pid = Fork()) == 0) { /* child */
  19. for (j = 0; j < nloops; j++) {
  20. fd = Tcp_connect(argv[1], argv[2]);
  21. /*
  22. * We want to see what happens to the server when it has
  23. * connections outstanding and an RST arrives for one of
  24. * them, before the connection is accepted.
  25. * With the XTI server, this should generate some
  26. * T_DISCONNECT events from t_accept(), which must be
  27. * handled correctly.
  28. *
  29. * We do this for every third connection from the third
  30. * client child. (Could add more command-line args ...)
  31. */
  32. if (i == 2 && (j % 3) == 0) {
  33. struct linger ling;
  34. ling.l_onoff = 1;
  35. ling.l_linger = 0;
  36. Setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
  37. Close(fd);
  38. /* and just continue on for this client connection ... */
  39. fd = Tcp_connect(argv[1], argv[2]);
  40. }
  41. Write(fd, request, strlen(request));
  42. if ( (n = Readn(fd, reply, nbytes)) != nbytes)
  43. err_quit("server returned %d bytes", n);
  44. Close(fd); /* TIME_WAIT on client, not server */
  45. }
  46. printf("child %d done\n", i);
  47. exit(0);
  48. }
  49. /* parent loops around to fork() again */
  50. }
  51. while (wait(NULL) > 0) /* now parent waits for all children */
  52. ;
  53. if (errno != ECHILD)
  54. err_sys("wait error");
  55. exit(0);
  56. }