tcprecv03p.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. int listenfd, connfd, n, justreadoob = 0;
  6. char buff[100];
  7. struct pollfd pollfd[1];
  8. if (argc == 2)
  9. listenfd = Tcp_listen(NULL, argv[1], NULL);
  10. else if (argc == 3)
  11. listenfd = Tcp_listen(argv[1], argv[2], NULL);
  12. else
  13. err_quit("usage: tcprecv03p [ <host> ] <port#>");
  14. connfd = Accept(listenfd, NULL, NULL);
  15. pollfd[0].fd = connfd;
  16. pollfd[0].events = POLLRDNORM;
  17. for ( ; ; ) {
  18. if (justreadoob == 0)
  19. pollfd[0].events |= POLLRDBAND;
  20. Poll(pollfd, 1, INFTIM);
  21. if (pollfd[0].revents & POLLRDBAND) {
  22. n = Recv(connfd, buff, sizeof(buff)-1, MSG_OOB);
  23. buff[n] = 0; /* null terminate */
  24. printf("read %d OOB byte: %s\n", n, buff);
  25. justreadoob = 1;
  26. pollfd[0].events &= ~POLLRDBAND; /* turn bit off */
  27. }
  28. if (pollfd[0].revents & POLLRDNORM) {
  29. if ( (n = Read(connfd, buff, sizeof(buff)-1)) == 0) {
  30. printf("received EOF\n");
  31. exit(0);
  32. }
  33. buff[n] = 0; /* null terminate */
  34. printf("read %d bytes: %s\n", n, buff);
  35. justreadoob = 0;
  36. }
  37. }
  38. }