sinktcp.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
  3. * Permission to use or modify this software and its documentation only for
  4. * educational purposes and without fee is hereby granted, provided that
  5. * the above copyright notice appear in all copies. The author makes no
  6. * representations about the suitability of this software for any purpose.
  7. * It is provided "as is" without express or implied warranty.
  8. */
  9. #include "sock.h"
  10. void
  11. sink_tcp(int sockfd)
  12. {
  13. int n, flags;
  14. if (pauseinit)
  15. sleep_us(pauseinit*1000);
  16. for ( ; ; ) { /* read until peer closes connection; -n opt ignored */
  17. /* msgpeek = 0 or MSG_PEEK */
  18. flags = msgpeek;
  19. oncemore:
  20. if ( (n = recv(sockfd, rbuf, readlen, flags)) < 0) {
  21. err_sys("recv error");
  22. } else if (n == 0) {
  23. if (verbose)
  24. fprintf(stderr, "connection closed by peer\n");
  25. break;
  26. #ifdef notdef /* following not possible with TCP */
  27. } else if (n != readlen)
  28. err_quit("read returned %d, expected %d", n, readlen);
  29. #else
  30. }
  31. #endif
  32. if (verbose)
  33. fprintf(stderr, "received %d bytes%s\n", n,
  34. (flags == MSG_PEEK) ? " (MSG_PEEK)" : "");
  35. if (pauserw)
  36. sleep_us(pauserw*1000);
  37. if (flags != 0) {
  38. flags = 0; /* no infinite loop */
  39. goto oncemore; /* read the message again */
  40. }
  41. }
  42. if (pauseclose) { /* pausing here puts peer into FIN_WAIT_2 */
  43. if (verbose)
  44. fprintf(stderr, "pausing before close\n");
  45. sleep_us(pauseclose*1000);
  46. }
  47. if (close(sockfd) < 0)
  48. err_sys("close error"); /* since SO_LINGER may be set */
  49. }