user6584
user6584

Reputation: 201

Segmentation fault on recv()

I am writing client/server programs on Unix in C, using send/recv. I am occasionally getting a segmentation fault from a recv call. The behavior is not perfectly reproducible; sometimes it happens, and sometimes the program runs to completion.

Any ideas what this could mean?

Upvotes: 2

Views: 4262

Answers (3)

user123
user123

Reputation: 1

This could also mean that the socket on the other side is closed before you write to it. That is a SIGPIPE that may be causing your application to close.

Upvotes: 0

caf
caf

Reputation: 239321

If the segmentation fault is in the recv() call itself, then that implies that the buffer you passed to recv() is not properly allocated or is not the size that you told recv() it was.

Upvotes: 6

paxdiablo
paxdiablo

Reputation: 882526

Well, it usually means you're receiving more data than your buffer has allowed for.

For example, if you malloc 20 bytes and recv 1000 bytes, you'll run into this problem.

Unfortunately, without seeing the code, and without some very rapid advances in the field of psychic debugging, we'll never know for sure *a.


*a That's a subtle hint to post the code, by the way :-)

Upvotes: 3

Related Questions