Skip to content

@benlangfeld /jruby_fail.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/env ruby
$:.push File.expand_path('../../lib', __FILE__)
require 'celluloid/io'
class Server
include Celluloid::IO
def initialize(host, port)
puts "*** Starting server on #{host}:#{port}"
@server = TCPServer.new(host, port)
run!
end
def finalize
puts "Server closing"
@server.close if @server
end
def run
loop { handle_connection! @server.accept }
end
def handle_connection(socket)
_, port, host = socket.peeraddr
puts "*** Received connection from #{host}:#{port}"
loop { puts socket.readpartial(4096) }
rescue EOFError
puts "*** #{host}:#{port} disconnected"
socket.close
end
end
class Client
include Celluloid::IO
def initialize(host, port)
puts "*** Connecting to echo server on #{host}:#{port}"
@socket = TCPSocket.new(host, port)
run!
end
def shutdown
@socket.close if @socket
end
def run
loop { puts @socket.readpartial 4096 }
rescue EOFError => e
puts "Client socket closed due to #{e.message}!"
terminate
end
end
server = Server.new("127.0.0.1", 1234)
client = Client.new("127.0.0.1", 1234)
client.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.