FD_SET has unknown sockfd

W

William

FD_SET( listener, &master );
fdmax = listener;


FD_ZERO( &readfds );
FD_ZERO( &master );
FD_SET( STDIN, &master ); // STDIN is now among the set of fds that
we pool for incoming data
FD_SET( listener, &master );
fdmax = listener;

for ( ;; ) {

if ( !FD_ISSET( STDIN, &master) && (peer.getBufferSTDIN() ==
false) ) {
FD_SET( STDIN, &master );
if ( fdmax < STDIN ) {
fdmax = STDIN;
cout << "fdmax < STDIN" << endl;
}
}
readfds = master; // since readfds change with every select,
// copy master into readfds to tell
select() which sockets we are
// interested in

// select returns the number of descriptors contained in the
descriptor sets
numFDInSet = select( fdmax+1, &readfds, NULL, NULL, &tv );


// ...
for ( int i = 0; i < fdmax + 1; i++ ) {
if (FD_ISSET(i, &readfds)) { // data coming in
from either STDIN or sockfds
if ( i == STDIN && (peer.getBufferSTDIN() ==
false) ) {
// ...
}
else if ( i == listener ) {
cout << "listener has sockfd:" << listener << endl;
// ...
}
else if ( i == sockfd ) {
FD_CLR( sockfd, &master );
if ( close(sockfd) != 0 )
{
cerr << "Error closing gossip
reply socket" << strerror(errno) << endl;
}
// ...


sockfd = peer.gossip( peerInfo );
FD_SET( sockfd, &master);
if ( fdmax < sockfd ) {
fdmax = sockfd;
}
else {
// ...
FD_CLR( sockfd, &master );
if ( close(sockfd) != 0 ) {
$
cerr << "Error closing
gossip reply socket" << strerror(errno) << endl; $
}

//...
sockfd = peer.gossip( peerInfo );
$
FD_SET( sockfd, &master);
if ( fdmax < sockfd ) {
fdmax = sockfd;
}
}
}
else {
// ...
FD_CLR( sockfd, &master );
if ( close(sockfd) != 0 ) {
cerr << "Error closing gossip
reply socket" << strerror(errno) << endl; $
}
}// else
}
else {
cout << endl << "received input from unknown
socket: " << i << endl << endl;

if ( FD_ISSET(listener, &readfds) ) {
cout << "listener socket " << i << " is in
the FD_SET, listener is socket " << listener << en$
}
else {
cout << "listener is not in the set" <<
endl;
}
}//else

My questions:
#1) Now focus on the last section. listener prints 5 (i.e. I am
always listening on socket 5), but I somehow got input coming in from
socket 7. I have not explicitly add socket 7 (i.e. i == 7) to my FD_SET.
How did socket 7 get added to FD_SET?

#2) Even more strangely, FD_ISSET(listener, &readfds) returns true,
meaning that listener (aka socket 5) is in my FD_SET. But if listener is
in my FD_SET, the case (i == listener) would have been executed; but
instead the control falls through to the else clause in #1.
Can you please explain this why the else clause is executed instead of (i
== listener)?

Thanks for your help. Any suggestion for a fix (i.e. I intend to listen
on socket 5 [ aka listener] for incoming input) is appreciated.
 
M

Malte Starostik

William schrieb:
[spaghetti code snipped]
My questions:
#1) Now focus on the last section. listener prints 5 (i.e. I am
always listening on socket 5), but I somehow got input coming in from
socket 7. I have not explicitly add socket 7 (i.e. i == 7) to my FD_SET.
How did socket 7 get added to FD_SET?

Hard to tell from the code. I'd start by refactoring it to remove the
excessive levels of indentation. Also it would be much more readable if
long lines were properly split and not auto-wrapped.
And then, the problem is off-topic in a C++-language newsgroup. You've
already posted it to comp.unix.programmer. If you absolutely feel the
need to post to multiple groups, then check if it's on topic in all of
them and CROSSpost, don't MULTIpost.

Cheers,
Malte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top