Note that there are some explanatory texts on larger screens.

plurals
  1. POListening socket connecting without accept being called on Linux
    primarykey
    data
    text
    <p>I am running code on Ubuntu Linux it is supposed to use a Set and select to check when a listening socket has activity (ie someone trying to connect) and let them connect, the trouble is select ALLWAYS returns 0, and when I try to connect it just connects straight away. but on the server Accept is never called as select always returns 0, so I am wondering what could cause this?</p> <pre><code>namespace SocketLib { const int MAX = FD_SETSIZE; class SocketSet { public: SocketSet(); void AddSocket( const Socket&amp; p_sock ); void RemoveSocket( const Socket&amp; 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, &amp;m_activityset, 0, 0, &amp;t ); #else if( m_socketdescs.size() == 0 ) return 0; return select( *(m_socketdescs.rbegin()), &amp;m_activityset, 0, 0, &amp;t ); #endif } inline bool HasActivity( const Socket&amp; p_sock ) { return FD_ISSET( p_sock.GetSock(), &amp;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&lt;sock&gt; m_socketdescs; #endif }; </code></pre> <p>is the code running the poll in case it helps</p> <p>Additional code is: </p> <pre><code>#include &lt;algorithm&gt; #include "SocketSet.h" namespace SocketLib { SocketSet::SocketSet() { FD_ZERO( &amp;m_set ); FD_ZERO( &amp;m_activityset ); } void SocketSet::AddSocket( const Socket&amp; p_sock ) { // add the socket desc to the set FD_SET( p_sock.GetSock(), &amp;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&amp; p_sock ) { FD_CLR( p_sock.GetSock(), &amp;m_set ); #ifndef WIN32 // remove the descriptor from the vector m_socketdescs.erase( p_sock.GetSock() ); #endif } } // end namespace SocketSet </code></pre> <p>also it is being used here { // define a data socket that will receive connections from the listening // sockets DataSocket datasock;</p> <pre><code> // detect if any sockets have action on them int i=m_set.Poll(); if( i &gt; 0 ) { // loop through every listening socket for( size_t s = 0; s &lt; 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-&gt;NewConnection( datasock ); } </code></pre> <p>as you can see, it wont do a .Accept until AFTER it has got activity from the select, but it never gets that far Bind and listen call is here template void ListeningManager::AddPort( port p_port ) { if( m_sockets.size() == MAX ) { Exception e( ESocketLimitReached ); throw( e ); }</p> <pre><code> // 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 ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload