简体   繁体   English

C套接字编程,select()和fd_set的问题

[英]C Socket Programming, problems with select() and fd_set

I'm learning my way about socket programming in C (referring to Beej). 我正在学习C语言中的套接字编程(指Beej)。

Here is a simple multi-user chat server i'm trying to implement: http://pastebin.com/gDzd0WqP 这是一个简单的多用户聊天服务器,我正在尝试实现: http//pastebin.com/gDzd0WqP

On runtime, it gives Bus Error. 在运行时,它会给出总线错误。 It's coming from the lines 68-78. 它来自68-78线。

Help me trace the source of the problem? 帮我跟踪问题的根源?

in fact, WHY is my code even REACHING that particular region? 事实上,为什么我的代码甚至到达那个特定区域? I've just run the server. 我刚刚运行服务器。 no clients have connected.. :@ 没有客户连接..:@

ps - i know my code is highly unreliable (no error checks anywhere), but i WILL do that at a later stage, i just want to TEST the functionality of the code before implementing it in all it's glory ;) ps - 我知道我的代码非常不可靠(在任何地方都没有错误检查),但我会在稍后阶段做到这一点,我只想在实现它的所有荣耀之前测试代码的功能;)

line 81 第81行

msg[MSG_SIZE] = '\0';` 

overruns your buffer. 超出你的缓冲区。 Make it 做了

msg[MSG_SIZE - 1] = '\0';` 

You also need to check the return value of all the calls that can fail, that's line 39,42,45,68 and 80 您还需要检查可能失败的所有呼叫的返回值,即39,42,45,68和80行

Edit: And if you'd checked for errors, likely you'd seen the accept() call fail, likely due to the socket not being in listen mode - that is, you're missing a call to listen() 编辑:如果你检查错误,可能你看到accept()调用失败,可能是由于套接字没有处于监听模式 - 也就是说,你错过了一个listen()的调用

Another thing to consider is that you can't necessarily copy fd_set variables by simple assignment. 另一件需要考虑的事情是,您不一定要通过简单的赋值来复制fd_set变量。 The only portable way to handle them is to regenerate the fd_set from scratch by looping over a list of active file descriptors each time. 处理它们的唯一可移植方法是每次循环遍历活动文件描述符列表,从头开始重新生成fd_set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM