Question

I am trying to make an interactive socket communication client program which sends out messages to a server when user asks it to and also receives incoming messages on the same socket (once connected). Since these messages may arrive out of order, and I'm using non-blocking IO, I intended to check for a flag at the end of each select if user has interrupted the program to enter more options.

For the first call to get_user_opts() , the function runs fine and takes input from the user, but for the subsequent call, it does not wait for user input.

Running it in gdb says:

getchar () at getchar.c:37
37  getchar.c: No such file or directory.
in getchar.c
(gdb) 
38  in getchar.c
(gdb) 
37  in getchar.c
(gdb) 
_IO_acquire_lock_fct () at libioP.h:984
984 libioP.h: No such file or directory.
in libioP.h
(gdb) 
985 in libioP.h
(gdb) 
getchar () at getchar.c:41
41  getchar.c: No such file or directory.
in getchar.c
(gdb) 
get_user_opts () at ../testmain.c:444
444     switch(user_opt){
(gdb) 
597             printf("\n Unknown Option. Exiting\n");
(gdb) print user_opt
$1 = 10 '\n'

The relevant code segment is:

void get_user_opts(){
/***************************************************************************/
/* User Interactivity variables                                            */
/***************************************************************************/
unsigned char user_opt;

/***************************************************************************/
/* Initialization started. Ask user for options                            */
/***************************************************************************/
printf("\n************************************\n Options: \n\t1 - Open Connection "\
        "\n\t2 - Send Register\n\t3 - Send Unregister\n\t4 - Send Add Session" \
        "\n\t5 - Send Delete Session\n\t6 - Send Startup done\n"\
        "**************************************\n");

//  fflush(stdin);
//scanf("%c", &user_opt);
user_opt = getchar();

switch(user_opt){
    case '1':...

The signal handler toggles a flag which is checked at the end of each while loop. Suspicious that the signal handler might be the culprit, I tried making that condition true without the handler, and yet, to no avail.

Any ideas?

Was it helpful?

Solution

Change:

user_opt = getchar();

to

user_opt = getchar();
while( user_opt != '\n' && getchar() != '\n' )
{
    // do nothing
}

This will remove any characters that follow that assigned to user_opt from teh line buffer - including the inevitable '\n'.

The console on most systems is line buffered, so getchar() will not return until an entire line is buffered (i.e. you have pressed ENTER); then you have at least two characters buffered = the one you entered and the newline from pressing ENTER. If you don't remove all buffered characters, getchar() will return immediately on a subsequent call. The loop continues to get characters until it finds a newline, but checks user_opt in case the user simply pressed ENTER.

OTHER TIPS

Use this:

unsigned char user_opt, tempChar;
while((tempChar = getchar())!='\n') user_opt = tempChar;
...

This will remove the newline character problem. It works because the while loop will make sure that the newline character is consumed by getchar() and does not create problems in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top