我正在尝试从服务器收到消息,但我遇到了一些问题。我确信我的服务器代码很好,因为它没有多路复用套接字,但是现在当我尝试使用select()时它没有收到任何东西。
while(1){
tmp_fds = read_fds;
if (select(fdmax + 1, &tmp_fds, NULL, NULL, NULL) == -1)
error((char *)"ERROR in select");
if (FD_ISSET (sockfd, &tmp_fds)){
memset(buffer, 0 , BUFLEN);
n = recv (sockfd,buffer,BUFLEN,0);
}
这是我从服务器接收的代码。我究竟做错了什么 ? 用于与服务器通信的套接字已在read_fds中。 BUFLEN是256.
答案 0 :(得分:1)
以这种方式试试
while(1)
{
tmp_fds = read_fds;
int ret=select(fdmax + 1, &tmp_fds, NULL, NULL, NULL);
if (ret > 0)
{
if (FD_ISSET (sockfd, &tmp_fds))
{
// there is no need for the memset
memset(buffer, 0 , BUFLEN);
n = recv (sockfd,buffer,BUFLEN,0);
}
}
else
if (ret < 0)
{
error((char *)"ERROR in select");
// here you must check what kind of error you received
// may be you need to close the socket and start over again
// return;
}
}
答案 1 :(得分:0)
在调用select之前,您应该清除FD结构并设置所需的标志。毕竟,当select返回时,这些位可能会从select更改,当你下次执行循环时,它们与之前的不同。
以下是我目前正在实施的用于我的服务器的示例,这肯定是有效的。
while(sigterm == false)
{
FD_ZERO(&mReaders);
FD_ZERO(&mWriters);
FD_ZERO(&mExceptions);
FD_SET(fileno(stdin), &mReaders);
FD_SET(serversocket, &mReaders);
pret = pselect(FD_SETSIZE, &mReaders, &mWriters, &mExceptions, NULL, &mSignalMask);
nErrorCode _code = errno;
if(pret == 0)
{
// Timeout occured, but we are not interested in that for now.
continue;
}
if(pret < 0)
{
switch(nErrorCode)
{
case EINTR:
cout << "Interrupted: " << pret << " errno: " << nErrorCode << endl;
break;
default:
{
cout << "Unknown retval from pselect: " << pret<< " errno: " << nErrorCode << endl;
}
}
continue;
}
if(FD_ISSET(fileno(stdin), &mReaders))
{
string s;
cin >> s;
cout << "stdin: " << s << endl;
continue;
}
if(FD_ISSET(serversocket, &mReaders))
{
// client connected
}
}