0

I've used this repository which is a Boost.Asio like structure that implements messaging and double ended queue and client and server communication in c++.

The problem is, it written for windows and in the file SimpleClient.cpp, reads keypress from command line, now I want to read command line keypress on Ubuntu terminal therefore I found this question on the stack which uses fd_set and I put it in a function and then in the SimpleClient file:

void task1_getkeyboard(CustomClient *c)
{
    struct termios oldSettings, newSettings;

    tcgetattr( fileno( stdin ), &oldSettings );
    newSettings = oldSettings;
    newSettings.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr( fileno( stdin ), TCSANOW, &newSettings );
    
    
    fd_set set;
    struct timeval tv;

    tv.tv_sec = 5;
    tv.tv_usec = 0;

    FD_ZERO( &set );
    FD_SET( fileno( stdin ), &set );

    int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );
    char cinif;

    if( res > 0 )
    {
    
        read( fileno( stdin ), &cinif, 1 );
        //printf( "Input available %c\n" , cinif);
            if ( cinif == 'p' ) {
            c->PingServer();
            cinif = '\0';
        }
    
    }
        else if( res < 0 )
        {
            perror( "select error" );
            //break;
        }
        else
        {
            printf( "Select timeout\n" );
        }
}

and I've put the function inside the while loop :

...
while (!bQuit)
    {
        task1_getkeyboard(&c);
    if (c.IsConnected())
    {
...

The problem is , It holds back the terminal until its timeout, for example on the successful connection, Client terminal should print Server Accepted Connection but it waits for 5 second(fd_set timeout).How can I fix this?

malloc
  • 544
  • 7
  • 17

0 Answers0