title: How do I clear the stdin in C?
body: <p>In a multi-client/server implementation I am connecting the client and server and then taking inputs from the client's stdin to send to server (otherwise if there's an incoming message from the server, I am performing a recv() and I am using select() to switch between the two). Now, as an example, I am giving an input "LOGIN IP PORT" and that connects me to the server. And then I am back to the client's command line again. </p>
  
  <p><strong>NOW</strong>, when I am back at the command line again after the login command, if I press enter without typing any specific command, the previous LOGIN command is executed again, it seems. I think the stdin is not being flushed. Although I have done <code>fseek(stdin,0,SEEK_END)</code>, it's still not getting flushed. The client side main loop code is as below:</p>
  
  <pre><code>while(TRUE) {
  
          fd_max = server_fd;
          FD_ZERO(&amp;sock_list);
          FD_SET(STDIN, &amp;sock_list);
          FD_SET(server_fd, &amp;sock_list);
  
          printf("\n[PA1-Client]$ ");
          fflush(stdout);
          fseek(stdin,0,SEEK_END);
  
          selret = select(fd_max + 1, &amp;sock_list, NULL, NULL, NULL);
          if(selret &lt; 0)
              perror("Select failed");
          else {
  
              int sock_index;
              for(sock_index = 0; sock_index&lt;=fd_max; sock_index++) {
  
                  if(FD_ISSET(sock_index, &amp;sock_list)) {
  
                      if(sock_index == STDIN) {
  
                          char* cmd = (char*) malloc(sizeof(char)*CMD_SIZE); 
  
                          if(fgets(cmd, CMD_SIZE - 1, stdin) == NULL)                 
                              exit(-1);
  
                          cmd[strcspn(cmd, "\n")] = '\0';                                       char* token[3];                          
                          tokenize(cmd, token, " ");
                          if(!strcmp(token[0], "LOGIN")) {
                              server_fd = clientLogin(token[1], token[2]);
                          }
                          else if(!strcmp(token[0], "REFRESH")) {
                              clientRefresh(token[0]);
                          }
                          else if(!strcmp(token[0], "LOGOUT")) {
                              clientLogout(token[0]);
                          }
                          else if(!strcmp(token[0], "EXIT")) {
                              clientExit(token[0]);
                              exit(0);
                          }
                          else if(!strcmp(token[0], "SEND")) {
                              clientSend(cmd);
                          }
                          else if(!strcmp(token[0], "BROADCAST")) {
                              clientBroadcast(cmd);
                          }
                          else if(!strcmp(token[0], "BLOCK")) {
                              clientBlock(cmd);
                          } 
                          else if(!strcmp(token[0], "UNBLOCK")) {
                              clientUnblock(cmd);
                          }
                          else if(!strcmp(token[0], "AUTHOR")) {
                              commonAuthor();
                          }
                          else if(!strcmp(token[0], "IP")) {
                              commonIP();
                          }
                          else if(!strcmp(token[0], "PORT")) {
                              commonPort();
                          }
                          else if(!strcmp(token[0], "LIST")) {
                              commonList();
                          }
  
                      }
                      else {
  
                          char* buffer = (char*)malloc(sizeof(char)*MSG_SIZE);
                          if(recv(server_fd, buffer, sizeof(buffer), 0) == 0) {
                              close(server_fd);
                              server_fd = 0;
                          }
                          else 
                              msgRecvEvent(buffer);
                      }
                  }
              }
          }
      }
  
      return 0;
  }
  </code></pre>