Solved

using sockets' select on the client side to check readability

Posted on 2001-06-22
3
284 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
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 …
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

730 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