Alternatively, you can use the macro fstatand S_ISSOCK.
int main(int argc, char *argv[])
{
int fds[2];
fds[0] = 0;
fds[1] = socket(AF_INET,SOCK_STREAM, 0);
for (int i = 0; i < 2; ++i)
{
struct stat statbuf;
if (fstat(fds[i], &statbuf) == -1)
{
perror("fstat");
exit(1);
}
if (S_ISSOCK(statbuf.st_mode))
printf("%d is a socket\n", fds[i]);
else
printf("%d is NOT a socket\n", fds[i]);
}
return(0);
}
source
share