开发者

Is there a way to close a Unix socket for only reading or writing?

Is there a way to only close "one end" of a TCP socket to cleanly indicate one side of a connection is done writing to the connection? (Just like you do with a pipe in every 开发者_如何学GoUnix pipe tutorial ever.) Or should I use some in-band solution like a sentinel value or some such?


You can shutdown a socket for read or write using the second parameter to the method:

shutdown(sock, SHUT_RD)
shutdown(sock, SHUT_WR)

If the server is doing the writing, and does a shutdown() for write, the client should get an end of file when it tries to read (rather than blocking and waiting for data to arrive). It will however still be able to write to the socket.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜