Hello, I was messing around with sockets in Ruby, and stumbled upon some sample code that I was trying to modify and break. I want to know why it broke.
Server:
require "socket"
dts = TCPServer.new('127.0.0.1', 20000)
loop do
Thread.start(dts.accept) do |s|
print(s, " is accepted\n")
s.write(Time.now)
print(s, " is gone\n")
s.close
end
end
Client who works:
require 'socket'
streamSock = TCPSocket.new( "127.0.0.1", 20000 )
streamSock.print( "Hello\n" )
str = streamSock.recv( 100 )
print str
streamSock.close
Customer who is broken
require 'socket'
streamSock = TCPSocket.new( "127.0.0.1", 20000 )
streamSock.print( "Hello\n" )
str=streamSock.read
print str
streamSock.close
I know that I do streamSock.printn’t need it (as well as a naming scheme that is not a ruby), but I don’t understand why it readdoesn’t work, but recvit does, why is it?
source
share