【问题标题】:Listening socket connecting without accept being called on Linux在 Linux 上不接受调用的情况下侦听套接字连接
【发布时间】:2011-10-02 19:15:45
【问题描述】:

我在 Ubuntu Linux 上运行代码,它应该使用 Set 并选择来检查侦听套接字何时有活动(即有人试图连接)并让他们连接,问题是 select ALLWAYS 返回 0,当我尝试连接它只是直接连接。 但是在服务器上 Accept 永远不会被调用,因为 select 总是返回 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 数据包;

    // 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( 端口 p_port ) { 如果(m_sockets.size() == MAX) { 异常 e(ESocketLimitReached); 抛出(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 );
}

【问题讨论】:

  • 你在哪里设置m_set

标签: c++ linux network-programming mud


【解决方案1】:

select() 需要最大的 fd+1。你给它最大的 fd,未修改。如果您在 Linux 而不是 Windows 上看到此错误,这是最可能的原因。

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2017-04-27
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多