sourceudp.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_udp(int sockfd) /* TODO: use sendto ?? */
  12. {
  13. int i, n, option;
  14. socklen_t optlen;
  15. pattern(wbuf, writelen); /* fill send buffer with a pattern */
  16. if (pauseinit)
  17. sleep_us(pauseinit*1000);
  18. for (i = 1; i <= nbuf; i++) {
  19. if (connectudp) {
  20. if ( (n = write(sockfd, wbuf, writelen)) != writelen) {
  21. if (ignorewerr) {
  22. err_ret("write returned %d, expected %d", n, writelen);
  23. /* also call getsockopt() to clear so_error */
  24. optlen = sizeof(option);
  25. if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
  26. &option, &optlen) < 0)
  27. err_sys("SO_ERROR getsockopt error");
  28. } else
  29. err_sys("write returned %d, expected %d", n, writelen);
  30. }
  31. } else {
  32. if ( (n = sendto(sockfd, wbuf, writelen, 0,
  33. (struct sockaddr *) &servaddr,
  34. sizeof(servaddr))) != writelen) {
  35. if (ignorewerr) {
  36. err_ret("sendto returned %d, expected %d", n, writelen);
  37. /* also call getsockopt() to clear so_error */
  38. optlen = sizeof(option);
  39. if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
  40. &option, &optlen) < 0)
  41. err_sys("SO_ERROR getsockopt error");
  42. } else
  43. err_sys("sendto returned %d, expected %d", n, writelen);
  44. }
  45. }
  46. if (verbose)
  47. fprintf(stderr, "wrote %d bytes\n", n);
  48. if (pauserw)
  49. sleep_us(pauserw*1000);
  50. }
  51. if (pauseclose) {
  52. if (verbose)
  53. fprintf(stderr, "pausing before close\n");
  54. sleep_us(pauseclose*1000);
  55. }
  56. if (close(sockfd) < 0)
  57. err_sys("close error"); /* since SO_LINGER may be set */
  58. }