tags:

views:

152

answers:

2

Hello

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 functionality of the code before implementing it in all it's glory ;)

+3  A: 

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

nos
Thanks, but the primary question is, why a runtime in THAT region, because only the server is running, no clients connected. Is this a possible case of unhandled errors?
wretrOvian
It's not only possible, it's exactly what's happening.
nos
ok, i'll work on them first. Thanks : )
wretrOvian
Very possible indeed. If `socket()` failed, it would return -1, and at that point all the FD_SET macros are probably into undefined behaviour. If `bind()` failed, your call to `select()` could well be into undefined behaviour. Also, you don't appear to have set a port number anywhere; what exactly are you trying to bind to?
crazyscot
The first thing I do after encountering a run time error in somethign I've written quick (ie, without checking for errors) is to go ahead and take the time to implement the error checks that I left out. Then I find the absolute first place where the program doesn't do exactly what I thought it should do.
nategoose
+1  A: 

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.

caf