使用 select 等待客户端套接字上的数据
有什么想法为什么当服务器在客户端等待 select 时写入套接字时, select 永远不会完成?
我正在使用 c 在套接字之间进行通信。我的客户端可以正常连接到我的服务器。
socket_desc=socket(AF_INET,SOCK_STREAM,0);//create the socket descriptor
client->address.sin_addr.s_addr = inet_addr(ipAddress);
client->address.sin_family = AF_INET;
client->address.sin_port = htons(port);
bind(socket_desc,&address,sizeof(address));
connect(socket_desc, &address, sizeof(address));
当我使用 recv 来阻止和侦听数据时,一切正常:
int bytesRead = 1;
while(bytesRead){
int bufsize=1024;
char *buffer=malloc(bufsize);
bytesRead = recv(socket_desc, buffer, bufsize, 0);
printf("CLIENT RECV: %s", buffer);
}
如果我尝试使用 select,它似乎没有读取任何数据。如果我将 STDIN 添加到 fd_set,我可以强制它从套接字读取,但 select 似乎不会从读取数据的 socket_desc 中触发...?
int running = 1;
while(running){
/* wait for something to happen on the socket */
struct timeval selTimeout;
selTimeout.tv_sec = 2; /* timeout (secs.) */
selTimeout.tv_usec = 0; /* 0 microseconds */
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(STDIN_FILENO, &readSet);//stdin manually trigger reading
FD_SET(socket_desc, &readSet);//tcp socket
int numReady = select(3, &readSet, NULL, NULL, &selTimeout);
//IT ONLY GETS PAST SELECT ON RETURN FROM THE KEYBOARD
if(numReady > 0){
char buffer[100] = {'\0'};
int bytesRead = read(socket_desc, &buffer, sizeof(buffer));
printf("bytesRead %i : %s", bytesRead, buffer);
if(bytesRead == 0){
running = FALSE;
printf("Shutdowning client.\n");
}
}
Any ideas why when the server writes a socket while the client is waiting on select, select never finishes?
I am using c to communicate between sockets. My client connects to my server fine.
socket_desc=socket(AF_INET,SOCK_STREAM,0);//create the socket descriptor
client->address.sin_addr.s_addr = inet_addr(ipAddress);
client->address.sin_family = AF_INET;
client->address.sin_port = htons(port);
bind(socket_desc,&address,sizeof(address));
connect(socket_desc, &address, sizeof(address));
When I use recv to block and listen for data, everything works fine:
int bytesRead = 1;
while(bytesRead){
int bufsize=1024;
char *buffer=malloc(bufsize);
bytesRead = recv(socket_desc, buffer, bufsize, 0);
printf("CLIENT RECV: %s", buffer);
}
If I try to use select, it doesn't seem to read any data. If I add STDIN to the fd_set, I can force it to read from the socket, but select doesn't seem to get triggered from the socket_desc reading in data...?
int running = 1;
while(running){
/* wait for something to happen on the socket */
struct timeval selTimeout;
selTimeout.tv_sec = 2; /* timeout (secs.) */
selTimeout.tv_usec = 0; /* 0 microseconds */
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(STDIN_FILENO, &readSet);//stdin manually trigger reading
FD_SET(socket_desc, &readSet);//tcp socket
int numReady = select(3, &readSet, NULL, NULL, &selTimeout);
//IT ONLY GETS PAST SELECT ON RETURN FROM THE KEYBOARD
if(numReady > 0){
char buffer[100] = {'\0'};
int bytesRead = read(socket_desc, &buffer, sizeof(buffer));
printf("bytesRead %i : %s", bytesRead, buffer);
if(bytesRead == 0){
running = FALSE;
printf("Shutdowning client.\n");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
select 的第一个参数应该是最大套接字 ID 加 1。所以在你的情况下,它应该是
Can you try with that and see if it work?
仅当您按下键盘上的某个键时才会出现此错误,因为 stdin 为 0,该值在 0 - (3 - 1) 范围内,这就是所检查的内容。如果将第一个参数设置为socket_desc+1,则应检查0 - (socket_desc)范围内的就绪套接字
The first parameter to select should be the maximum socket id plus 1. So in your case, it should be
Can you try with that and see if it works?
The reason it only gets when you press a key on the keyboard is because stdin is 0, which would be within 0 - (3 - 1) range, which is what is checked. If you set the first parameter to socket_desc+1, then 0 - (socket_desc) range should be checked for ready sockets