套接字上的 select() (问题)

发布于 2024-08-13 16:36:42 字数 1117 浏览 2 评论 0原文

最近我完成了部分代码。

它确实有效,但 select() 效果不好。
当它收到服务器的最后回复时,它开始重复最后的回复字符串,并在回复字符串的开头添加一些奇怪的字符。 所以看看这个:

:[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

第一个字符串是服务器的最后回复。所以接下来的字符串只是一些垃圾。 我尝试在一些搜索引擎中查找信息,但没有成功。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-08-20 16:36:42

这不是 select 有问题,而是代码中的逻辑有问题(毫不奇怪)。

如果 select 因为 stdin 可读而返回,那么您仍然会查看 c[0] (从最后一次服务器读取) - 即使您没有从服务器读取这次,它仍然是 \n,因为您从未重置过它。

移动这段代码:

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

else if (rc == 1) { 块内。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文