2007/8/13, David -- <neko18 / gmail.com>:
> I have two programs, a server and a client. I want to send two messages
> to the client when the client connects to the server, with a delay in
> between. However, both messages seem to arrive at the client at the same
> time. Here are the programs:
>
> ## simpleserver.rb
> ## run this first
> require "socket"
> server = TCPServer.new(2000)
> while connection = server.accept
>   connection.write "one\n"
>   connection.flush
>   sleep 1
>   connection.write "two\n"
>   connection.close
> end
>
> ## client.rb
> ## run this second
> require "socket"
> sock = TCPSocket.open("localhost", 2000)
> while not sock.eof?
>   print sock.read
> end

As far as I can see you read the whole stream in one go with
sock.read.  Try this instead

sock.each_line do |line|
  puts line
end

Kind regards

robert