Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
BasicSocket#close_write
() -> void
basicsocket.close_write => nil
ext/socket/basicsocket.c
View on GitHub ↗
static VALUE
bsock_close_write(VALUE sock)
{
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (!(fptr->mode & FMODE_READABLE)) {
return rb_io_close(sock);
}
/* Flush any buffered (sync == false) data before shutting down the
* write side. Otherwise the buffered bytes are silently dropped here,
* and a subsequent #close would try to flush them into the now
* shutdown(SHUT_WR) socket and fail with EPIPE. This matches the
* write-only branch above, which flushes via rb_io_close(). */
rb_io_flush(sock);
shutdown(fptr->fd, SHUT_WR);
fptr->mode &= ~FMODE_WRITABLE;
return Qnil;
}
Disallows further write using shutdown system call.
Example
UNIXSocket.pair {|s1, s2| s1.print "ping" s1.close_write p s2.read #=> "ping" s2.print "pong" s2.close p s1.read #=> "pong" }
See also
#close_read
#connect_address
.for_fd
#do_not_reverse_lookup
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0