sourcetcp.c 1.6 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. source_tcp(int sockfd)
  12. {
  13. int i, n, option;
  14. socklen_t optlen;
  15. char oob;
  16. pattern(wbuf, writelen); /* fill send buffer with a pattern */
  17. if (pauseinit)
  18. sleep_us(pauseinit*1000);
  19. for (i = 1; i <= nbuf; i++) {
  20. if (urgwrite == i) {
  21. oob = urgwrite;
  22. if ( (n = send(sockfd, &oob, 1, MSG_OOB)) != 1)
  23. err_sys("send of MSG_OOB returned %d, expected %d",
  24. n, writelen);
  25. if (verbose)
  26. fprintf(stderr, "wrote %d byte of urgent data\n", n);
  27. }
  28. if ( (n = write(sockfd, wbuf, writelen)) != writelen) {
  29. if (ignorewerr) {
  30. err_ret("write returned %d, expected %d", n, writelen);
  31. /* also call getsockopt() to clear so_error */
  32. optlen = sizeof(option);
  33. if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
  34. &option, &optlen) < 0)
  35. err_sys("SO_ERROR getsockopt error");
  36. } else
  37. err_sys("write returned %d, expected %d", n, writelen);
  38. } else if (verbose)
  39. fprintf(stderr, "wrote %d bytes\n", n);
  40. if (pauserw)
  41. sleep_us(pauserw*1000);
  42. }
  43. if (pauseclose) {
  44. if (verbose)
  45. fprintf(stderr, "pausing before close\n");
  46. sleep_us(pauseclose*1000);
  47. }
  48. if (close(sockfd) < 0)
  49. err_sys("close error"); /* since SO_LINGER may be set */
  50. }