Sign in to follow this  

File Descriptor question

This topic is 3858 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
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

This topic is 3858 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this