我正在尝试从服务器接收消息,但遇到一些问题。我确信我的服务器代码很好,因为它无需多路复用套接字即可工作,但现在当我尝试使用 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中。 布弗伦是 256。
试试这个方法
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;
}
}
在调用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
}
}