Solved

using sockets' select on the client side to check readability

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fabric 1 31
Cisco AP to get ip from DHCP 10 96
exclude a user from a deny permisssion 4 56
Windows 7 Internet issue 14 53
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 …
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

809 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