daytimeudpcli3.c 736 B

12345678910111213141516171819202122232425262728293031
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. int sockfd, n, nq;
  6. char recvline[MAXLINE + 1];
  7. socklen_t salen;
  8. struct sockaddr *sa;
  9. if (argc != 3)
  10. err_quit("usage: a.out <hostname or IPaddress> <service or port#>");
  11. sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen);
  12. printf("sending to %s\n", Sock_ntop_host(sa, salen));
  13. Sendto(sockfd, "", 1, 0, sa, salen); /* send 1-byte datagram */
  14. n = Recvfrom(sockfd, recvline, MAXLINE, MSG_PEEK, NULL, NULL);
  15. Ioctl(sockfd, FIONREAD, &nq); /* check FIONREAD support */
  16. printf("%d bytes from PEEK, %d bytes pending\n", n, nq);
  17. n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
  18. recvline[n] = 0; /* null terminate */
  19. Fputs(recvline, stdout);
  20. exit(0);
  21. }