I have read this post Determine between socket and fd, and the answer recommends creating a structure with two fields, is there another way to test if a descriptor is a socket or a regular file on windows ?
-
Unlike other OS systems, Windows does not use file descriptors for sockets. Sockets are their own distinct type. Why are you mixing file descriptors and sockets together such that you need to differentiate between them in the first place? Can you show a code example of what you are trying to solve? – Remy Lebeau Sep 06 '16 at 03:39
2 Answers
You could try something that should only work on a socket, and see if it fails with ENOTSOCK. getsockname()
for example.

- 305,947
- 44
- 307
- 483
-
Yes good idea, I have checked _fstat but st_mode has only three possible values according to https://msdn.microsoft.com/en-us/library/221w8e43.aspx – Bionix1441 Sep 05 '16 at 14:42
-
Unfortunately S_ISSOCK(st_mode) is defined for linux but not for windows – Bionix1441 Sep 05 '16 at 14:45
-
-
this could help:
replace stdin stdout stderr with a socket…
http://www6.uniovi.es/cscene/CS5/CS5-05.html
int exec_comm_handler( int sck )
{
close(0); /* close standard input */
close(1); /* close standard output */
close(2); /* close standard error */
if( dup(sck) != 0 || dup(sck) != 1 || dup(sck) != 2 ) {
perror("error duplicating socket for stdin/stdout/stderr");
exit(1);
}
printf("this should now go across the socket...\n");
execl( "/bin/sh", "/bin/sh", "-c", "/path/to/redirected_program" );
perror("the execl(3) call failed.");
exit(1);
}
Our 'comm handler' first closes the file descriptors for standard input, standard output, and standard error. It then uses dup(2) to duplicate the socket file handle. dup(2) will duplicate the given file descriptor and return the duplicate as the next available descriptor. Since 0, 1, and 2 are the next available descriptors, they should be the returned duplicates. Now operations on stdin/stdout/stderr [0/1/2] will act upon the socket instead of the original stdin/stdout/stderr.
I was unaware that this technique (calling dup(2) on a socket file descriptor) was possible untill seeing code written by Martin Mares.

- 311
- 1
- 10