问题
Can I bind a file descriptor returned by open call to a socket?
I am trying to do something like,
filefd = open("path",O_RDWR);
...
bind (filefd, (struct sockaddr *) &servaddr, sizeof(servaddr));
connfd = accept (filefd, (struct sockaddr *) &cliaddr, &clilen);
Why does the accept call return -1?
回答1:
From an applications point of view, the difference is how you create and use the descriptor. Some system-calls can take any kind of descriptor, while others require a specific type of descriptor.
In your case the bind call would have returned -1 too, if you checked for the error. When a system-call returns -1 you should check errno to see what went wrong. You can use strerror to get a printable string of the error, or perror to print it directly.
来源:https://stackoverflow.com/questions/22499675/difference-between-file-descriptor-and-socket-file-descriptor