daytimeudpcli1.c 559 B

1234567891011121314151617181920212223242526
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. int sockfd, n;
  6. char recvline[MAXLINE + 1];
  7. socklen_t salen;
  8. struct sockaddr *sa;
  9. if (argc != 3)
  10. err_quit("usage: daytimeudpcli1 <hostname/IPaddress> <service/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, 0, NULL, NULL);
  15. recvline[n] = '\0'; /* null terminate */
  16. Fputs(recvline, stdout);
  17. exit(0);
  18. }