Wednesday, February 07, 2007

a simple tcp server

In the past, I've found it really useful to have a simple server that is able to listen on a TCP socket and print out everything it receives. It's a great debugging tool.


The following ruby snippet (adapter from an example in 'Programming Ruby') does just that:


require 'socket'
port = ARGV[0] || 80
server = TCPServer.new('localhost', port )
while( session = server.accept)
while !session.eof?
puts session.gets
end
end

Probably cooler would be if it took everything on STDIN and echo-ed it back to the socket as well.


Update: Thanks pooja. I couldn't think of netcat at the time but that does exactly what I want. I'm such an idiot for not remembering.

No comments: