Search code examples
csocketsstdoutstdin

Standard input and standard output with select() in C


i'm doing a client-server project for university. For the client-side i have to use the select() system call. First of all I need to print ">" and then wait, using select(), if socket is ready for reading a notification from server or stdin ready for reading from terminal. When i try to print ">", i have this problem:

input(socket or stdin)
>

but i would like to have:

>
input(socket or stdin).

Is there someone that could help me?? Here is part of my code:

...
...
FD_SET(sd, &master);
FD_SET(std_in, &master);
fdmax = sd; 

for(;;){        
    printf(">");

    read_fds = master;

    ret = select(fdmax+1, &read_fds,NULL, NULL,NULL);
    check_error_rs(ret,0);


    for(cont = fdmax + 1; cont >= 0; cont--){   
        if(FD_ISSET(cont, &read_fds)){
            if(cont == std_in){
                scanf("%s",send_buf);
                if(strcmp(send_buf,"!help") == 0){
                    .....
                }

                if(strcmp(send_buf,"!who") == 0){
                    .....
                }
            }
            if(cont == sd){
                check_notify();

            }

        }
    }
}

Solution

  • use fflush(stdout) before select()

    read_fds = master;
    
    fflush(stdout);
    
    ret = select(fdmax + 1, &read_fds, NULL, NULL, NULL);