Echo server: Difference between revisions

(Added PHP implementation)
Line 634:
 
=={{header|Ruby}}==
{{incorrect|Ruby|You cannot send two lines on the same connection and have them both echoed.}}
Note: largely untested: may not handle multiple simultaneously connections well.
<lang ruby>require 'socket'
 
server@socket = TCPServer.new('localhost'"", 12321)
while (session = server.accept)
@socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
client_port, client_host = session.peeraddr[1..2]
 
puts "activity from #{client_host}:#{client_port}"
def line = connection(session.gets)
Thread.new {
if line.nil?
client_port, client_host = session.closepeeraddr[1..2]
puts ("activity fromConnected: #{client_host}:#{client_port}")
else
session.puts(line)
while(msg = session.gets)
end
session.puts(linemsg)
end
session.close
puts("Disconnected: #{client_host}:#{client_port}")
}
end #connection
 
while true
if(newSocket = @socket.accept)
connection(newSocket)
end
end</lang>
 
Anonymous user