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
Version using Kernel::fork (new process) instead of Thread::fork (new thread) - MRI-Ruby only has green-threads, it can be better to use child-processes than child-threads in some situations)