开发者

C Socket Programming, problems with select() and fd_set

I'm learning my way about socket programming in C (referring to Beej).

Here is a simple multi-user chat server i'm trying to implement: http://pastebin.com/gDzd0WqP

On runtime, it gives Bus Error. It's coming from the lines 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 functional开发者_如何学编程ity of the code before implementing it in all it's glory ;)


line 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

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()


Another thing to consider is that you can't necessarily copy fd_set variables by simple assignment. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜