Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

using sockets' select on the client side to check readability

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
phirephly
Asked:
phirephly
  • 2
1 Solution
 
phirephlyAuthor Commented:
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
 
superschlonzCommented:
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
 
phirephlyAuthor Commented:
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now