Advertisement

berkely sockets and stdin causing problems?

Started by December 11, 2003 10:31 PM
2 comments, last by AcidInjury 21 years, 2 months ago
I am writing the client side of a server program. The program is text based and written for linux. After connecting and doing a few send() and recv() function calls for login and password, the main program loop starts. The loop begins like this.

do
{
       // Display prompt

        printf("Enter Command-> ");

        bzero(&ClientMsg, sizeof(MSGSTRUCT)); // Zero out message buffer


        // Get user input

        gets(in_buff);
        ...

The problem I am having is that for some reason the gets() function call returns immediately and the loop continues. This only happens on the first iteration of the loop, after which things works as expected. Anyone know why gets() is getting confused? Thanks if you can help. Will
The answer is probably no, but: Are you doing any C++ style console stuff (via cin and/or cout)?


"Sneftel is correct, if rather vulgar." --Flarelocke
Advertisement
The problem I am having is that for some reason the gets() function call returns immediately and the loop continues.

And you were not curious enough to check out what the data read by gets() was ? tsk. tsk. tsk.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
actually i did check the contents of in_buff, and it''s arbitrary (like it should be).

What I don''t get is why gets() is returning immediately. I know that when getchar() is used, and you type more than one char than any other calls to getchar() will use the remaining chars until there are no more. Is this the same occurance?

...

Ok the problem was that characters were being left in the input buffer. Now I flush the buffer at the beggining of each iteration.

Guess sometimes you just have to write your problem out to other people for it to make sense.

Will

This topic is closed to new replies.

Advertisement