Separate the two stream directions
A stream connection has two directions that can finish independently. Python socketpair creates two connected local sockets, allowing one endpoint to send hello, finish its sending direction and still receive a reply. This exercises stream-socket API behavior without contacting a network service. On the intended Unix environment, socketpair uses a local Unix-domain connection, so the experiment does not capture TCP packets or directly demonstrate the timing of FIN segments. Keep the API observation distinct from protocol-level packet evidence.
Read a reply after SHUT_WR
The client sends its request and calls shutdown(SHUT_WR), indicating that it will send no further bytes in that direction. The server reads until recv returns empty bytes, then sends accepted in the reverse direction. Because the client has not closed its socket, it can receive that response. Both reads use loops rather than assuming that one recv returns a complete logical message. A short local transfer often arrives together, but that observation must not become a framing assumption.
import socket
client, server = socket.socketpair()
try:
client.sendall(b"hello")
client.shutdown(socket.SHUT_WR)
chunks = []
while True:
data = server.recv(1024)
if not data: break
chunks.append(data)
assert b"".join(chunks) == b"hello"
server.sendall(b"accepted")
server.shutdown(socket.SHUT_WR)
reply = []
while True:
data = client.recv(1024)
if not data: break
reply.append(data)
print(b"".join(chunks), b"".join(reply))
assert b"".join(reply) == b"accepted"
finally:
client.close(); server.close()Distinguish end of stream from an empty message
An empty bytes result from stream recv indicates that the receiving direction has reached an orderly end, not that an empty application message has arrived. Protocols needing multiple messages require their own framing, such as lengths or delimiters. This example uses end of sending as the request delimiter, which is unsuitable unchanged for a sequence of requests on the same connection. Once that direction has been shut down, the client cannot start sending a new request through it.
Keep shutdown, close and failures distinct
Shutdown selects a communication direction, whereas close releases the application’s descriptor reference. Duplicated descriptors can complicate assumptions about when the underlying socket is finally released. Abrupt peer failure or a broken network can produce errors or prolonged waiting rather than the orderly end used here. Production code therefore needs deadlines and cancellation in addition to half-close semantics. The final close calls are resource cleanup; they are not the mechanism that allows the reverse-direction reply to arrive.
The example deliberately uses very short byte strings. Sending a large request synchronously before allowing the peer to read can block on socket-buffer capacity. A larger demonstration needs independently progressing endpoints, such as separate threads or asynchronous I/O. This buffering issue is separate from half-close semantics, so do not interpret a blocked large send as evidence that reverse-direction reception after shutdown is impossible.