在Linux上监听套接字连接而不接受调用
Listening socket connecting without accept being called on Linux
我在Ubuntu Linux上运行代码,它应该使用Set和select来检查侦听套接字何时有活动(即有人试图连接)并让他们连接,问题是选择ALLWAYS返回0,当我尝试连接时,它只是直接连接。但在服务器上接受从来没有被称为选择总是返回0,所以我想知道什么会导致这一点?
namespace SocketLib
{
const int MAX = FD_SETSIZE;
class SocketSet
{
public:
SocketSet();
void AddSocket( const Socket& p_sock );
void RemoveSocket( const Socket& p_sock );
inline int Poll( long p_time = 0 )
{
// this is the time value structure. It will determine how long
// the select function will wait.
struct timeval t = { 0, p_time * 1000 };
// copy the set over into the activity set.
m_activityset = m_set;
// now run select() on the sockets.
#ifdef WIN32
return select( 0, &m_activityset, 0, 0, &t );
#else
if( m_socketdescs.size() == 0 ) return 0;
return select( *(m_socketdescs.rbegin()), &m_activityset, 0, 0, &t );
#endif
}
inline bool HasActivity( const Socket& p_sock )
{
return FD_ISSET( p_sock.GetSock(), &m_activityset ) != 0;
}
protected:
// a set representing the socket descriptors.
fd_set m_set;
// this set will represent all the sockets that have activity on them.
fd_set m_activityset;
// this is only used for linux, since select() requires the largest
// descriptor +1 passed into it. BLAH!
#ifndef WIN32
std::set<sock> m_socketdescs;
#endif
};
是运行投票的代码,如果它有帮助的话
附加代码为:
#include <algorithm>
#include "SocketSet.h"
namespace SocketLib
{
SocketSet::SocketSet()
{
FD_ZERO( &m_set );
FD_ZERO( &m_activityset );
}
void SocketSet::AddSocket( const Socket& p_sock )
{
// add the socket desc to the set
FD_SET( p_sock.GetSock(), &m_set );
// if linux, then record the descriptor into the vector,
// and check if it's the largest descriptor.
#ifndef WIN32
m_socketdescs.insert( p_sock.GetSock() );
#endif
}
void SocketSet::RemoveSocket( const Socket& p_sock )
{
FD_CLR( p_sock.GetSock(), &m_set );
#ifndef WIN32
// remove the descriptor from the vector
m_socketdescs.erase( p_sock.GetSock() );
#endif
}
} // end namespace SocketSet
也在这里使用{//定义一个数据套接字,接收来自监听的连接//套接字DataSocket datasock;
// detect if any sockets have action on them
int i=m_set.Poll();
if( i > 0 )
{
// loop through every listening socket
for( size_t s = 0; s < m_sockets.size(); s++ )
{
// check to see if the current socket has a connection waiting
if( m_set.HasActivity( m_sockets[s] ) )
{
try
{
// accept the connection
datasock = m_sockets[s].Accept();
// run the action function on the new data socket
m_manager->NewConnection( datasock );
}
正如你所看到的,,它不会做一个。accept直到它从选择中获得活动之后,但它从来没有到那一步绑定和监听呼叫在这里模板无效ListeningManager::AddPort(port p_port){if(m_sockets.size() == MAX){异常(ESocketLimitReached);Throw (e);}
// create a new socket
ListeningSocket lsock;
// listen on the requested port
lsock.Listen( p_port );
// make the socket non-blocking, so that it won't block if a
// connection exploit is used when accepting (see Chapter 4)
lsock.SetBlocking( false );
// add the socket to the socket vector
m_sockets.push_back( lsock );
// add the socket descriptor to the set
m_set.AddSocket( lsock );
}
select()要求最大的fd+1。你给它最大的未修改的fd。如果你在Linux上看到这个错误,而不是在Windows上,这是最有可能的原因。
相关文章:
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 构造函数采用 Base&不被调用
- 为什么线程不接受此输入?
- 重载类方法的不明确调用
- 函数不接受 X 参数,函数使用默认参数
- C++ boost::asio::ip::tcp::acceptor 有时不接受连接器?
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 不能调用构造函数
- 为什么我不能调用 onFire();
- B不接受8作为输入的是什么?C++
- 对重载函数find_first_not_of的不明确调用
- 意外的 IConnectionPointImpl::不建议调用 Windows Embedded Compact 7
- C++数组输入不接受一定数量的整数
- C++概念assignable_from不接受 const&-return 运算符=
- 斯堪夫不接受输入
- C++字符数组不接受超过 4 个字符的输入
- CIN不接受C++中带有空格的输入?
- 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- 在Linux上监听套接字连接而不接受调用
- 当C++调用python时,不接受返回值