select() for blocking socket in windows -
void csocket::waitforconnetion() { csocket* psocket = null; fd_set readfds; readfds.fd_count = 1; readfds.fd_array[0] = m_hsocket; timeval timeout; zeromemory(&timeout, sizeof(timeout)); timeout.tv_sec = 10; while(!g_bquit) { int iselect = select(null, &readfds, null, null, &timeout); if (iselect == socket_error) { // error break; } else { if (iselect == 0) { // timeout } else { assert(iselect == 1); socket new_sock = accept(m_hsocket, null, null); if (new_sock != invalid_socket) { psocket = new csocketex(new_sock); // create new thread process connection request psocket } } continue; } }
hi,all
csocket socket wrapper.
using blocking socket in programm,calling take function blocked when want exit thoroughly, have terminate thread calling accept.i want utilize select function above.but select goes non-blocking socket, there problems or concerns using blocking socket under such environment above?with ejp's help.i modified code below.is there error? , said if utilize way closing socket handle,i must pass socket handle other thread close it,is safty operator 1 sokcet in different threads?
void csocket::waitforconnetion() { csocket* psocket = null; timeval timeout; zeromemory(&timeout, sizeof(timeout)); timeout.tv_sec = 10; while(!g_bquit) { fd_set readfds; readfds.fd_count = 1; readfds.fd_array[0] = m_hsocket; int iselect = select(null, &readfds, null, null, &timeout); if (iselect == socket_error) { // error break; } else { if (iselect == 0) { // timeout } else { assert(iselect == 1); socket new_sock = accept(m_hsocket, null, null); if (new_sock != invalid_socket) { psocket = new csocketex(new_sock); // create new thread process connection request psocket } } continue; } } windows sockets
No comments:
Post a Comment