Jump to content
  • Advertisement
Sign in to follow this  
md_lasalle

File Descriptor question

This topic is 4138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wondering if it's possible that linux/win32 can return 0 as a newly created socket ? Is fd 0 reserved for the system ? or it is possible to get a filedescriptor with value 0 ?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by md_lasalle
I'm wondering if it's possible that linux/win32 can return 0 as a newly created socket ?

Is fd 0 reserved for the system ? or it is possible to get a filedescriptor with value 0 ?


In linux, file descriptor 0 is stdin, with 1 and 2 being stdout and stderr. I'm haven't used such low level calls under windows so I don't know about how it arranges things.

In linux, it is possible to get a socket mapped to file descriptor 0 by using:

close(0);
dup(socketDescriptor);


I believe linux usually allocates the lowest unused file descriptor when a new one is needed, but I can only find that alluded to here, but not on the socket() page.

As for windows, this page mentions that:
Quote:

Socket handles may take any value in the range 0 to INVALID_SOCKET-1.

Share this post


Link to post
Share on other sites
Yes, under LINUX, 0 is a perfectly valid socket handle. Try this code:


#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>

int main() {
close(0);
int sock = socket(AF_INET, SOCK_STREAM, 0);
printf("%d\n", sock);
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!