views:

48

answers:

0

Hello I've been messing around with Sockets in Ruby some and came across some example code that I tried modifying and broke. I want to know why it's broken.

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 that works:

require 'socket'  
streamSock = TCPSocket.new( "127.0.0.1", 20000 )  
streamSock.print( "Hello\n" )  
str = streamSock.recv( 100 )  
print str  
streamSock.close 

Client that is broken

require 'socket'  
streamSock = TCPSocket.new( "127.0.0.1", 20000 )  
streamSock.print( "Hello\n" )  
str=streamSock.read #this line modified
print str  
streamSock.close 

I know that the streamSock.print is unnecessary (as well as the naming scheme being non-ruby) but I don't understand why read doesn't work while recv does, Why is this?