require "socket"
server = TCPServer.open(2626)
loop do
Thread.fork(server.accept) do |client|
client.puts("Hello, I'm Ruby TCP server", "I'm disconnecting, bye :*")
client.close
end
end
require "socket"
s = TCPSocket.open("localhost", 2626)
while line = s.gets
puts "received : #{line.chop}"
end
s.close
.. just 4 fun ;)
require "socket"
server = TCPServer.open(2626)
loop { Thread.fork(server.accept) { |client| client.puts("Hello, I'm Ruby TCP server", "I'm disconnecting, bye :*") or client.close }}
require "socket"
s = TCPSocket.open("localhost", 2626)
while line = s.gets do puts "received : #{line.chop}" end
s.close
How's about the evented option? ... not that many more lines of code really
TCP server:
TCP client:
Why ?
Well, it's all the rage (or was), it's very fast, it doesn't use a lot of memory (hovered around 30K) compared to
what it's processing, and there's no worries about threading and forking and all those entail.
Sure, you are not going to traipse off to the bonneville salt flats with this server and break any speed records,
but after trying all of the other options this seems to work the best for pushing data into a server for various
purposes. It's amazing what 50 lines of ruby code, not counting eventmachine itself, and *nix can do. Also,
all of my testing was done with ruby MRI.
fin