1

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.

NOW, 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 fseek(stdin,0,SEEK_END), it's still not getting flushed. The client side main loop code is as below:

while(TRUE) {

        fd_max = server_fd;
        FD_ZERO(&sock_list);
        FD_SET(STDIN, &sock_list);
        FD_SET(server_fd, &sock_list);

        printf("\n[PA1-Client]$ ");
        fflush(stdout);
        fseek(stdin,0,SEEK_END);

        selret = select(fd_max + 1, &sock_list, NULL, NULL, NULL);
        if(selret < 0)
            perror("Select failed");
        else {

            int sock_index;
            for(sock_index = 0; sock_index<=fd_max; sock_index++) {

                if(FD_ISSET(sock_index, &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;
}
Manic
  • 103
  • 1
  • 8
  • How about reading character by character until you get a newline? But only do that if you're certain there *are* characters to read (like a newline) or it will block. – Some programmer dude Oct 08 '17 at 18:39
  • Possible duplicate of [How to clear input buffer in C?](https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c) – J...S Oct 09 '17 at 02:01
  • My recollections is that the only _reliable_ way to flush `stdin` on any Unix/Linux system is to read the input until there is no more. Tricks like `fseek(stdin...)` are not very portable, and don't even work on all Linux variants. – Kevin Boone Oct 09 '17 at 07:13

0 Answers0