Solved

using sockets' select on the client side to check readability

Posted on 2001-06-22
3
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

630 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