using select to read from socket and stdin

匆匆过客 提交于 2019-12-04 11:28:10
fabrizi0

Your problem is in the select().
The first parameter is not the number of file descriptors you are passing in read_fds, but it's the highest socket ID + 1.

From the man page:

The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. (Example: If you have set two file descriptors "4" and "17", nfds should not be "2", but rather "17 + 1" or "18".)

So in your code, instead of '2', try passing 's+1'.

You need to specify the highest file descriptor to select:

if (select(s + 1,&read_fds,NULL,NULL,NULL) == -1){

select() needs to know the number of file descriptors that it is supposed to watch.

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