difference between file descriptor and socket file descriptor

丶灬走出姿态 提交于 2021-01-27 13:21:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!