Maybe I've gotten my sockets programming way mixed up, but shouldn't something like this work?
srv = TCPServer.open(3333)
client = srv.accept
data = ""
while (tmp = client.recv(10))
data += tmp
end
I've tried pretty much every other method of "getting" data from the client TCPSocket, but all of them hang and never break out of the loop (getc, gets, read, etc). I feel like I'm forgetting something. What am I missing?
In order for the server to be well written you will need to either:
read(size)
instead of recv(size)
. read
blocks until the total amount of bytes is received.recv
until you receive a sequence of bytes indicating the communication is over.read
will return with partial data or with 0 and recv
will return with 0 size data data.empty?==true
.select
in order to get a timeout when no communication was done after a certain period of time. In which case you will close the socket and assume every data was communicated.Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With