From: James Tucker Date: 2008-01-05T00:01:41+09:00 Subject: Re: problem with sockets STDIN.gets blocks hard on windows - that is, it blocks at the "c level" which blocks all running green threads. The result is this 'lag' behavior - in fact what is happening normally is that you're not getting any running threads or socket reads except in the time between gets returning a value, and being called again. If you avoid $stdin, you will find that the flow is smooth. Oh, and $stdout blocks too... There's a video here that sort of demonstrates this in a visual way: http://blog.ra66i.org/wp-content/network_rotator_fun.swf One side is an echo server, which reverses the string it echos, and the other is a generator, which generates strings with a moving # in them. "I" and "O" have meaning local to the respective application. The right hand side is the generator, the left hand side is the reversing echo server. Those were actually built on top of EM, but it's the same issue you see. On 4 Jan 2008, at 06:00, ian@frogcottage.demon.co.uk wrote: > Hi > I've just started using ruby and thought I'd do some experimenting > with TCPSocket and TCPServer > > I've got a small problem, in that the program I've written runs fine > on Linux, but runs with a lag on windows. By this I mean that when you > type into the client on std input in Linux, what you type is > automatically displayed. > However in windows nothing happens until you type and send something > else. Like it's caught in a buffer and needs to be flushed. I've tried > flushing the output stream as you would in Java, but nothing seems to > work > If anyone could give me some advice I would be eternally grateful. > I've searched around the net, but to no avail. > > Client > > require 'socket' > require 'thread' > > class Client > > def initialize > > host = 'localhost' > port = 40000 > > s = TCPSocket.new(host, 40000) > > > t = Thread.new do > > while(true) > > line = s.gets > > if(line == nil) > puts("Program is exiting") > break > end #of if > > $stdout.flush > > > > puts "Received: #{line}" > > > > $stdout.flush > > end #of while > > s.close > > end #of thread > > while(true) > > puts("About to run stdin") > > line = $stdin.gets > > s.puts(line) > > > end #of while > puts 'QUIT' > > t.join > > s.close > > Process.exit > > end #of con > > > > > end #of class > > > st = Client.new > > > > Server > > require 'socket' > require 'thread' > > class ServerThread > > def initialize > > t = TCPServer.new('localhost', 40000) > > > while(true) > > go = t.accept > > > s = Thread.new(go) do |v| > > > while(true) > > > # data = "none" > > > data = v.gets > > > v.write(data) > > > > if(data == nil) > puts("From inside if") > self.closeSocket(v) > > end #of if > > > end # while > > end #of thread >