c - Network Programming: what happen if network disconnect after select and before send -


i need implement timeout socket send()/recv() system calls should return after timeout. , think standard method should be, call select()/pool() before send()/recv(). below example code copy ffmpeg source code:

    int ff_network_wait_fd(int fd, int write)                                                {                                                                                         int ev = write ? pollout : pollin;                                                       struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };                              int ret;                                                                              ret = poll(&p, 1, 100);                                                                return ret < 0 ? ff_neterrno() : p.revents &                 (ev | pollerr | pollhup) ? 0 :     averror(eagain);     }                                                                                      int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, aviointerruptcb *int_cb)     {                                                                                    int ret;                                                                         int64_t wait_start = 0;                                                           while (1) {                                                                          if (ff_check_interrupt(int_cb))                                                      return averror_exit;                                                         ret = ff_network_wait_fd(fd, write);                                             if (ret != averror(eagain))                                                          return ret;                                                                  if (timeout > 0) {                                                                   if (!wait_start)                                                                     wait_start = av_gettime();                                                   else if (av_gettime() - wait_start > timeout)                                        return averror(etimedout);                                               }                                                                            }                                                                            }      static int tcp_write(urlcontext *h, const uint8_t *buf, int size)                {                                                                                    tcpcontext *s = h->priv_data;                                                    int ret;                                                                          if (!(h->flags & avio_flag_nonblock)) {                                              ret = ff_network_wait_fd_timeout(s->fd, 1, h->rw_timeout, &h->interrupt_callback);             if (ret)                                                                             return ret;                                                              }                                                                                ret = send(s->fd, buf, size, 0);                                                 return ret < 0 ? ff_neterrno() : ret;                                        }                                                                            

seems tcp_write() call ff_network_wait_fd_timeout() prepare environment. makes me confusion that, if network cable unplugged after ff_network_wait_fd()(this should call poll() system call), should send() return -1, or still blocked? much.

  1. you don't need poll() implement timeout recv(). call setsockopt() option so_rcvtimeo , deal ewouldblock errno timeout.

  2. you need use poll() send() timeout.

  3. a broken tcp connection can detected trying write it. there no network event triggers poll() event.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -