简体   繁体   中英

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?

如果分段错误在recv()调用本身中,那么这意味着传递给recv()的缓冲区没有正确分配,或者不是你告诉recv()它的大小。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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