tags:

views:

27

answers:

1

Is there any reason why the server process does not give any output in the following code?

require 'socket'

server = TCPServer.open(3000)
loop{
    puts "waiting for connection"
    client = server.accept
    puts "connection!"
    client.close
    puts "client closed"
}

I connect via:

nc localhost 3000

in a windows cygwin environment.

No output is visible in the server terminal. Should I maybe flush something somewhere?

A: 

You expect the server to respond to the client with "connection!", is that correct? If so, your problem is that #puts redirects its output to STDOUT when called on Kernel. If we call #puts on the client's TCPSocket, it would write the output to the TCPSocket IO:

require 'socket'

server = TCPServer.open(3000)
loop{
    puts "waiting for connection"
    client = server.accept
    client.puts "connection!"
    client.close
    puts "client closed"
}

And then the clients end would look like this:

$ nc localhost 3000
connection!
Sirupsen
No, I ment for all the output to go to the server terminal. It looks like it's something with cygwin not printing to terminal before I interrupt the server process. Running in a native windows terminal gives the desired output. Thanks for the answer though.
ddccffvv