Reputation: 5452
I am building a client that can recieve information from the server and from the user (stdin
), so I am using select to monitor both. What happens is that is a keyboard input is monitored the I send a message to the client and get one back, no problem. But when the server send a message nothing happens, and I don't know why. Is using select()
the proper way to do so?
Here's my code:
void readSocket(fd_set tempfd) {
const char * tweet, * inMessage;
if (FD_ISSET(srverfd,&tempfd)) {
inMessage = getMessage();
printSystemMessages(inMessage);
}
if (FD_ISSET(STDIN_FILENO,&tempfd)) {
tweet = getUserTweet();
sendMessage(tweet);
inMessage = getMessage();
if (strcmp(inMessage,OK) != 0) {
printSystemMessages(inMessage);
}
if (strcmp(inMessage,EXIT) == 0) {
return;
}
}
return;
}
int main (int argc, char *argv[] ){
int value;
bool clientON = false;
fd_set tempfd;
if(establishConnection(argv[2],argv[3])){
cerr << "usage: failed to make connection" << endl << "exiting..." << endl;
exit(EXIT_FAILURE);
}
cout << "Connected successfully" << endl;
sendMessage("CONNECT "+clientName); //Connect
if(strcmp(getMessage(),OK) == 0){
build_select_list();
printSystemMessages("Welcome!");
clientON = true;
cout<< man <<endl;
}
while(clientON){
tempfd = inputFdSet;
printTweetFormat("TweetMy:");
value = select(maxSock, &tempfd, NULL, NULL, NULL);
if (value < 0) {
perror("select");
exit(EXIT_FAILURE);
}
if (value == 0) {
continue;
}
else {
readSocket(tempfd);
}
}
close(srverfd);
return 0;
}
Upvotes: 1
Views: 3918
Reputation: 21351
This might help you - pass maxSock+1 instead of maxSock to your select() call;
From the man page on select()
nfds is the highest-numbered file descriptor in any of the three sets,
plus 1.
Upvotes: 1