daytimetcpcli.c 769 B

123456789101112131415161718192021222324252627282930313233
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. int sockfd, n, npend;
  6. char recvline[MAXLINE + 1];
  7. socklen_t len;
  8. struct sockaddr_storage ss;
  9. if (argc != 3)
  10. err_quit("usage: a.out <hostname or IPaddress> <service or port#>");
  11. sockfd = Tcp_connect(argv[1], argv[2]);
  12. len = sizeof(ss);
  13. Getpeername(sockfd, (SA *)&ss, &len);
  14. printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len));
  15. for ( ; ; ) {
  16. if ( (n = Recv(sockfd, recvline, MAXLINE, MSG_PEEK)) == 0)
  17. break; /* server closed connection */
  18. Ioctl(sockfd, FIONREAD, &npend); /* check FIONREAD support */
  19. printf("%d bytes from PEEK, %d bytes pending\n", n, npend);
  20. n = Read(sockfd, recvline, MAXLINE);
  21. recvline[n] = 0; /* null terminate */
  22. Fputs(recvline, stdout);
  23. }
  24. exit(0);
  25. }