• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# (non)blocking?

## Recommended Posts

xSuiCidEx    122
im setting up a dos program to be the server for a game im working on, and i want to use nonblocking mode and not use windows messages to notify me of writing/reading access to the sockets because i plan to port it to linux later down the road alright, so everything else works fine, but within the infinite while loop, i have: for(int i = 1; i <= 10; i++){ //do some code } and the for loop doesn''t execute the code at all....its like it''s ignoring every single for loop in the program, but doing evetything else fine....i was wondering if this is maybe because of nonblocking not being totally set up right? and its blocking and not causing my loops to execute? thats the only thing i can think of...heres how im setting nonblocking mode for my listen socket: unsigned long Options = 1; ioctlsocket(ListenSock, FIONBIO, &Options); this is called right after i create the socket using socket()....after them calls i then bind() and listen() and go through the infinite while loop and it works fine for accepting new connectionsthis is in the infinite loop btw) FD_SET(ListenSock, &RecvFDS); select(0, &RecvFDS, &SendFDS, &ErrorFDS, &TVal); if(FD_ISSET(ListenSock, &RecvFDS)){ OpenSock = GetOpenSock(); FD_CLR(Socks[OpenSock].Socket, &Socks[OpenSock].RecvFDS); FD_CLR(Socks[OpenSock].Socket, &Socks[OpenSock].SendFDS); Socks[OpenSock].SINLen = sizeof(sockaddr_in); Socks[OpenSock].Socket = accept(ListenSock, (struct sockaddr*)&Socks[OpenSock].SIN, &Socks[OpenSock].SINLen); ioctlsocket(Socks[OpenSock].Socket, FIONBIO, &Options); FD_SET(Socks[OpenSock].Socket, &Socks[OpenSock].RecvFDS); FD_SET(Socks[OpenSock].Socket, &Socks[OpenSock].SendFDS); Socks[OpenSock].Active = TRUE; printf("Accepted: %i\n", OpenSock); } then it loops through all the active sockets using a for loop, if they are active it checks them for data to write/read, but the loop isn''t being preformed...i use the same checking process for the listensock as all the other client sockets i hope this is enough information for someone to help me with...or maybe a few of u have had this same problem thanx

##### Share on other sites
Guest Anonymous Poster
After looking at your code, it seems that your array ''Sock'' contains a class or struct that has two fd_sets (RecvFDS and SendFDS) in it, and that you''re adding that socket to it''s own set. This is not the way the fd_sets are supposed to be used at all.

You should have only one fd_set for Recv, Send, and possibly Exceptions. Use FD_ZERO to clear each set before a call to select(), and then loop over your array ''Sock'' and use FD_SET to add them to the Recv and Send sets. Then call select() with the sets. I believe you have to FD_ZERO and reset the fd_sets before every call to select(), because the function itself changes the data in the sets in such a way that they can''t be reused.

So:
 fd_set recv, send, except; FD_ZERO(&recv); FD_ZERO(&send); FD_ZERO(&except); for(int i = 0; i < numSocks; i++) {   FD_SET(Sock.Socket, &recv);   FD_SET(Sock[i].Socket, &send);   FD_SET(Sock[i].Socket, &except); } FD_SET(ListenSock, &recv); select(0, &recv, &send, &except, &tv); if(FD_ISSET(ListenSock, &recv)) {   AcceptNewConenction(); } for(int i = 0; i < numSocks; i++) {   if(FD_ISSET(Sock[i].Socket, &recv))     RecvData(Sock[i]);   if(FD_ISSET(Sock[i].Socket, &send))     SendData(Sock[i]);   if(FD_ISSET(Sock[i].Socket, &except))     HandleException(Sock[i]); }

I hope to hell that looks right after I post...

- genovov