start_connect.c 842 B

1234567891011121314151617181920212223242526272829303132
  1. #include "web.h"
  2. void
  3. start_connect(struct file *fptr)
  4. {
  5. int fd, flags, n;
  6. struct addrinfo *ai;
  7. ai = Host_serv(fptr->f_host, SERV, 0, SOCK_STREAM);
  8. fd = Socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  9. fptr->f_fd = fd;
  10. printf("start_connect for %s, fd %d\n", fptr->f_name, fd);
  11. /* 4Set socket nonblocking */
  12. flags = Fcntl(fd, F_GETFL, 0);
  13. Fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  14. /* 4Initiate nonblocking connect to the server. */
  15. if ( (n = connect(fd, ai->ai_addr, ai->ai_addrlen)) < 0) {
  16. if (errno != EINPROGRESS)
  17. err_sys("nonblocking connect error");
  18. fptr->f_flags = F_CONNECTING;
  19. FD_SET(fd, &rset); /* select for reading and writing */
  20. FD_SET(fd, &wset);
  21. if (fd > maxfd)
  22. maxfd = fd;
  23. } else if (n >= 0) /* connect is already done */
  24. write_get_cmd(fptr); /* write() the GET command */
  25. }