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.
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.