By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

FD_SET has unknown sockfd

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.