client.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. Write(fd, request, strlen(request));
  22. if ( (n = Readn(fd, reply, nbytes)) != nbytes)
  23. err_quit("server returned %d bytes", n);
  24. Close(fd); /* TIME_WAIT on client, not server */
  25. }
  26. printf("child %d done\n", i);
  27. exit(0);
  28. }
  29. /* parent loops around to fork() again */
  30. }
  31. while (wait(NULL) > 0) /* now parent waits for all children */
  32. ;
  33. if (errno != ECHILD)
  34. err_sys("wait error");
  35. exit(0);
  36. }