0

I'm having issues with my client side implementation of client server chat program where multiple clients connect. The issue is that i'm coming across is that how do i let the client know its okay to type something in? Currently, my printf statement is not being outputted to the screen. Is there a way i can notify the client that it's okay to type without using a new line?

here is the relevant code

client side

while(1) {
  printf(">"); //this isn't being outputted

  fd_set rfds;
  FD_ZERO(&rfds);

  FD_SET(serverSocket, &rfds);
  FD_SET(0, &rfds);

  if(select(serverSocket+1, &rfds, NULL, NULL, NULL) < 0) {
      perror("select");
      exit(-1);
  }

  if (FD_ISSET(serverSocket, &rfds)) {
     //recv data from server
  }
  else if (FD_ISSET(0, &rfds)) {
     //read keyboard
  }
}
theStig
  • 612
  • 1
  • 13
  • 33
  • `printf(">");` -->> `fprintf(stderr, ">\n");` This is being outputted. – wildplasser Feb 10 '13 at 00:55
  • 2
    Could you try with a `fflush(stdout);` after the `printf`? – Ganesh Feb 10 '13 at 00:57
  • @wildplasser post your solution so that i may mark is as the answer. Bit of a hack, but i suppose it works in this case. Unless someone has a better alternative. – theStig Feb 10 '13 at 01:03
  • @theStig: No, I wont. This really is trivial stuff: diagnostic output **should** go to stderr. and stdstuff is line buffered. – wildplasser Feb 10 '13 at 01:25
  • BTW: `if(select(serverSocket+1, &rfds, NULL, NULL, NULL) < 0) {` is wrong. select() could return -1/EAGAIN , etc. – wildplasser Feb 10 '13 at 01:33

1 Answers1

1

Since stdout is line-buffered by default, you have at least two choices:

  1. Explicitly flush after writing to stdout without a newline. Try fflush(stdout); as suggested by Ganeesh.
  2. Turn off buffering on stdout for your entire program. Try setvbuf(stdout, NULL, _IONBF, 0);. You can see an example of this here: Is it safe to disable buffering with stdout and stderr?
Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Wow, i need to take a break. was thinking fflush is a bad idea since im working with stdin... but i'm not. That printf is going to stdout. Thanks – theStig Feb 10 '13 at 01:30