Using TCPSocket
, I need to socket.puts "foobar"
, and then socket.close
to let the socket in the other side socket.read
the message.
Is there a way to send or receive a message though a socket, but without closing the socket, which mean I can send message again without creating a socket again?
p.s Something like websocket
It should not be neccessary to close the connection in order for the other party to read it.
send
should transfer the data over the conection immediately. Make sure the other party is reading from the socket.If you traverse up the super class chain you will eventually see that you inherit from
IO
. MostIO
objects, in Ruby, buffer the data to be more efficient writing and reading from disk.In your case, the buffer wasn't large enough (or enough time didn't pass) for it to flush out. However, when you closed the socket, this forced a flush of the buffer resources.
You have a few options:
IO#flush
.IO#sync=
totrue
. You can check the status of yourIO
object's syncing usingIO#sync
; I'm guessing you'd seesocket.sync #=> false
BasicSocket#send
which will call POSIXsend(2)
; since sockets are initialized withO_FSYNC
set, the send will be synchronous and atomic.