select() on socket (trouble)

338 views Asked by At

Recently I have done this part of code.

It does work, but select() works bad.
When it has got last reply from server, it begins repeating last reply string with some strange characters in the beginning of reply string. So look at this:

:[email protected] PRIVMSG testuser1 :VERSION
�C���C��[email protected] PRIVMSG testuser1 :VERSION
�C���C��[email protected] PRIVMSG testuser1 :VERSION
�C���C��[email protected] PRIVMSG testuser1 :VERSION
�C���C��[email protected] PRIVMSG testuser1 :VERSION

The first string is the last reply from server. So next strings are just some junk. I tried to look for information in some search engines, but no luck.

1

There are 1 answers

1
caf On BEST ANSWER

It's not select that has the problem, it's the logic in your code (unsurprisingly).

If select returns because stdin is readable, then you still look at c[0] (from the last server read) - and even though you didn't read from the server this time, it's still \n, because you never reset it.

Move this bit of code:

    if (c[0] == '\n' || c[0] == '\0') {                                        
        buf[--buflen] = '\0';                                                  
        handleMessage(buf, buflen);  /* Just print message */
        buf_do_clean = 1;                                                      
    }                                                                          

inside the else if (rc == 1) { block.