c语言read()返回-1,errno:Connection reset by peer

问题描述:服务器端执行完send()后调用close()关闭socket,然后exit()正常退出。客户端read()函数返回-1,errno104 :   Connection reset by peer,查了一下原因:

这意味着收到了TCP RST,并且连接现在已经关闭,这可能发生在另一方崩溃,然后返回,或者如果它调用close()在套接字上。

解决方法:服务器在调用close()函数前,调用shutdown(socket, 2),先关闭socket的读写功能,客户端read()正常,问题解决。

close()和shutdown()的区别:

请参考:https://blog.csdn.net/xyyaiguozhe/article/details/30252559

猜你喜欢

转载自blog.csdn.net/busai2/article/details/81350973