theStig
theStig

Reputation: 610

c socket: flushing data to let client know its okay to type

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
  }
}

Upvotes: 0

Views: 247

Answers (1)

John Zwinck
John Zwinck

Reputation: 249193

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?

Upvotes: 1

Related Questions