Solved

using sockets' select on the client side to check readability

Posted on 2001-06-22
3
268 Views
Last Modified: 2013-12-23
here is the main loop of the client side of my client/server app. What I'd like it to do is keep looping and checking if there's something to read. If the write flag is set, then I'd like to to write that time. Select will keep read from clogging up my loop, right? What appears to be wrong with this segment?  What it's doing is printing "Got this number somewhere <some funny number>" about a trillion times (like prolly each time the loop goes) instead of just the times when the server is passing it information. thanks!


        time(&tloc);
        tend = tloc+120;

        while (running)  {
            FD_ZERO(&readmes);
            FD_SET(sockfd,&readmes);
            if(select(sockfd+1,&readmes,NULL,NULL,&timeout) < 0)  {
                printf("Select error 1\n");
                close(sockfd);
                return(1);
            }
            if(FD_ISSET(sockfd,&readmes))  {
                if ((readn(sockfd, (char *) &num, sizeof(int))) < 0)  {
                    perror("str_cli: readn error");
                    return;
                }
            }

            printf("Got this number somewhere: %d\n",num);

            if(write_flag)  {
                if (writen(sockfd,(char *)&num,sizeof(int)) != (sizeof(int)))  {
                    perror("str_cli: writen error on socket");
                    return;
                }
            }

            time(&tloc);
            if (tend <= tloc)
                running = 0;
        }
0
Comment
Question by:phirephly
  • 2
3 Comments
 

Author Comment

by:phirephly
ID: 6219862
alright, pencils down. here's why I was getting the repeated message: in the select statement, I just needed sockfd, not sockfd+1. Taking off the +1 gave me the results I wanted, when a number is typed in a different client attached to the same server, it pops up over on this client. But now I have a new problem. As you can see, for now, I'm using a the clock to terminate the loop. when 120 seconds have passed, the program should exit. It used to do this, but now that I have the select working properly, I've lost my timer. Once two minutes have gone by, I have to send the client one more number before the client will quit. It's almost like the read is still waiting for data (even tho it's not supposed to, right?). If there isn't any data to be read, then the FD_ISSET if statement should fail, and we should check the clock again, right? oh gee... the printf statement will print a thousand blank responses. I'll put that in the if statement with the read. Anyways, how come the loop is waiting for data to terminate? I thought this was one of the points of using select... thanks and ttyl,

mich
0
 
LVL 3

Accepted Solution

by:
superschlonz earned 200 total points
ID: 6220405
I assume you write an interactive client program. So the user of your program
wants to control the program so you should also read from the standard input.

This example should read numbers from standard input and send them to the
server and send also every two minutes a zero to the server. If the user types
"quit" it should quit the program.

#include <unistd.h>

time(&tloc);
tend = tloc+120;

while (running)  {
  FD_ZERO(&readmes);
  FD_SET(sockfd,&readmes);
  /* add the standard input */
  FD_SET( STDIN_FILENO, &readmes );
  if(select(sockfd+1,&readmes,NULL,NULL,&timeout) < 0)  {
    printf("Select error 1\n");
    close(sockfd);
    return(1);
  }
  /* timing */
  time( &tloc );
  if( tloc > tend ) {
    tend += 120;
    printf( "another two minutes\n" );
    /* send a zero every two minutes */
    num = 0;
    write_flag = 1;
  }
  timeout.tv_sec = tend-tloc;

  /* can we read from socket ? */
  if(FD_ISSET(sockfd,&readmes))  {
    if ((readn(sockfd, (char *) &num, sizeof(int))) < 0)  {
      perror("str_cli: readn error");
      return;
    }
    printf("Got this number somewhere: %d\n",num);
  }
  /* has the user entered something ? */
  if( FD_ISSET( STDIN_FILENO, &readmes ) {
    char line[100];
    if( fgets( line, sizeof(line), stdin ) == NULL ) {
      /* got an EOF */
      return;
    }
    if( !strncmp( line, "quit", 4 ) ) {
      /* user want's to quit */
      running = 0;
    } else {
      /* assume the user typed a number */
      num = atoi( line );
      if( num != 0 ) {
        /* number is not zero so we send it */
        write_flag = 1;
      }
    }
  }

  if(write_flag)  {
    if (writen(sockfd,(char *)&num,sizeof(int)) != (sizeof(int)))  {
      perror("str_cli: writen error on socket");
      return;
    }
    /* clear the write flag */
    write_flag = 0;
  }
}
0
 

Author Comment

by:phirephly
ID: 6719645
Good job. I just didn't know what I was looking at. Thanks for your help. Sorry about the delay in the reward. I lost this question and finally found how to look up the ones I still had pending.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now