From msdn: (about the return value of "select" function):

Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition.


What does it mean the structures are updated? How can I find out which sockets meet the conditions?

realcr.
Posted on 2004-10-30 16:21:00 by realcr
The fd_set array is updated. Any socket which does not match the state it is supposed to be in (ready for read, write, etc) is removed from the array in which it is initially placed by you using FD_SET.
You can then check if it still exists in the array by looping through it, or using __WSAFDIsSet which is exported by wsock32.dll and ws2_32.dll.

Note that in many examples in C/C++ you will use use of FD_SET, FD_ISSET. Those are actually standard macros declared in standard Windows includes. Here they are:


typedef struct fd_set {
u_int fd_count;
SOCKET fd_array[FD_SETSIZE];
} fd_set;

#define FD_CLR(fd,set) do { u_int __i;\
for (__i = 0; __i < ((fd_set *)(set))->fd_count ; __i++) {\
if (((fd_set *)(set))->fd_array[__i] == (fd)) {\
while (__i < ((fd_set *)(set))->fd_count-1) {\
((fd_set*)(set))->fd_array[__i] = ((fd_set*)(set))->fd_array[__i+1];\
__i++;\
}\
((fd_set*)(set))->fd_count--;\
break;\
}\
}\
} while (0)
#endif

#define FD_ISSET(fd, set) __WSAFDIsSet((SOCKET)(fd), (fd_set *)(set))

#define FD_SET(fd, set) do { \
if (((fd_set *)(set))->fd_count < FD_SETSIZE) \
((fd_set *)(set))->fd_array[((fd_set *)(set))->fd_count++]=(fd);\
}while (0)


As you can see, fd_set is nothing more, nothing less than an array of SOCKET type variables (SOCKET is actually a typedef for int) with a counter of how many elements are in that array. FD_SETSIZE is usually equal to 64, but that you can change.

So when you call select, say with a pointer to "readset", which will be an fd_set structure, select will remove any elements from the array which do not have any data to be read, and will only leave the ones that you can read from.

So you would so something like:


readset dd 4, 0, 0, 0, 0

; put four sockets in the array, to be tested for readability
mov dword ptr [readset+04h], handle_of_socket1
mov dword ptr [readset+08h], handle_of_socket2
mov dword ptr [readset+0Ch], handle_of_socket3
mov dword ptr [readset+10h], handle_of_socket4

invoke select, 0, OFFSET readfs, 0, 0, 0

; loop through readset, and check which sockets can be read
lea edx, dword ptr [readset+04h]
mov ecx, dword ptr [readset+00h]
jecxz quit
check:

cmp [edx], handle_of_socket1
je readsocket1
cmp [edx], handle_of_socket2
je readsocket2
cmp [edx], handle_of_socket3
je readsocket3
cmp [edx], handle_of_socket4
je readsocket4

add edx, 4
dec ecx
ja check
quit:


something like tat...
Posted on 2004-10-30 18:43:28 by comrade
Tnx comrade.
Now everything is clear.

realcr.
Posted on 2004-10-31 14:53:09 by realcr