linux - Socket Select() is working but Poll() is not working correctly -


i calling function eventonsocket() again , again check if there event on sockets.

my code working fine if use select if use poll code not working correctly. in case of poll, getting timeout error.

here working code select.

int eventonsocket()  {     int retval = -1;     int socketmax = -1;     struct timeval tv;     tv.tv_sec = 15; //sec     tv.tv_usec = 0; //microsec     fd_zero(&m_rfds);      for(int = 0; < m_socklist.size(); i++)      {         int socketid = task->m_socklist.at(i);         if(socketmax < socketid)             socketmax = socketid;         fd_set(socketid, m_rfds);     }     if(socketmax > 0) {         retval = select(socketmax+1, &m_rfds, null, null, &tv);         return retval;     }     else         return -1; } 

but when use poll in same way timeout error. in poll assigning socket fds once in vector when call function first time , call function again , again check event on sockets.

here code poll.

int eventonsocket() {     /* assign socket fds once in vector when function called first time only*/     if(true == assignsocktonlyonce)     {                assignsocktonlyonce = false;          for(int i=0; < m_socklist.size(); i++)          {             int32_n socketid = m_socklist.at(i);             struct pollfd pfd;             pfd.fd = socketid;             pfd.events = pollin;              ufds.push_back(pfd);         }     }     retval = poll(&ufds[0],(nfds_t)ufds.size(), 15000);     if (retval == -1) {             log_err("===  error in  polling === ");      }     else if (retval == 0) {             log_err("timeout occurred!  no data after 15 seconds.");     }     else {             for(int = 0; < ufds.size(); i++) {                 if (ufds[i].revents & pollin )                         log_err("read socket fd:%d",ufds[i].fd);             }     }     return retval; }  ufds vector defined in .h file std::vector<struct pollfd> ufds ; 

in poll assigning socket fds once in vector when call function first time , call function again , again check event on sockets.

that explain why these 2 code paths behave differently. make poll code rebuild ufds every time sake of checking.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -