File Descriptor question
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 ?
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.
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement