• 0

C++ fd_set (windows/linux)


Question

I'm having an issue with fd_set's under linux

in windows if i need to directly access an fd_set or see how many items are in there i can just use

fd_set test;

test.fd_array[0]; or test.fd_count;

however none of this functionality seems to be available for the linux version of fd_set's is it called something else or do i have to implement my own file descriptor set?

cheers

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

As far as I'm aware, something like test.fdcount doesn't exist. There are however useful macros for adding and removing descriptors from sets as the above poster mentioned but you will have to keep track of the largest file descriptor yourself.

To give a sockets example, which is where I've almost exclusively used fd_sets. Normally I'd do something along the lines of this:

fd_set readFds;
int fdmax;

FD_ZERO(&readFds);

// Open socket blah blah

FD_SET( socket, &readFds);
SetMaxFd( socket );

etc etc etc

	for(;; )
	{
		// Then call select
		select( fdmax + 1, &readFds, NULL, NULL, NULL);

		// Then check if your socket is in the set
		if ( FD_ISSET(socket, &readFds) )
		{
			// Read event on socket, i.e connection closed or receiving data
		}
	}

void SetMaxFd( int newfd)
{
	if ( newfd > fdmax)
	{
		fdmax = newfd;
	}
}

Let me know if anything's unclear. Then obviously you can add support for write events (in the case of non-blocking socket) or error events.

Also remember that select will modify your fd_set with which descriptors are set so you'll need to set these each time.

Link to comment
Share on other sites

  • 0
Are you sure you're using fd_set right? On linux you may need to do something different (see: http://linux.die.net/man/3/fd_set).

Yeah i checked that site they don't seem to have it so was wondering why considering its in winsock2

As far as I'm aware, something like test.fdcount doesn't exist. There are however useful macros for adding and removing descriptors from sets as the above poster mentioned but you will have to keep track of the largest file descriptor yourself.

To give a sockets example, which is where I've almost exclusively used fd_sets. Normally I'd do something along the lines of this:

snip

Let me know if anything's unclear. Then obviously you can add support for write events (in the case of non-blocking socket) or error events.

Also remember that select will modify your fd_set with which descriptors are set so you'll need to set these each time.

thanks for the information however i'm familiar with file descriptors under windows and they appeared to be the same in linux however

i was just pointing out that winsock has this information available and linux doesn't appear to

here is a snippet from WinSock2.h

typedef struct fd_set {
		u_int fd_count;			   /* how many are SET? */
		SOCKET  fd_array[FD_SETSIZE];   /* an array of SOCKETs */
} fd_set;

i could implement my own method but i was wondering if it had a different name that i couldn't find (couldn't find anything on the net and no intellisense available)

i have all the socket related stuff setup i was just hoping to take chunks of users and place them in groups and then be able to access that group and see who is linked in each group without having to loop through all descriptors but if its not available i can add it myself i was just hoping linux would already have it available :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.