Usage:
Open 2 terminals, run server.rb
in one and client.rb
in the other. The tcp socket file descriptor will be created in the client, passed to the server, and then used in the server to read from the HTTP server.
Usage:
Open 2 terminals, run server.rb
in one and client.rb
in the other. The tcp socket file descriptor will be created in the client, passed to the server, and then used in the server to read from the HTTP server.
#!/usr/bin/env ruby | |
require 'socket' | |
socket_path = File.expand_path(File.dirname(__FILE__), '.sock') | |
UNIXSocket.open(socket_path) { |c| | |
# open a socket and pass the FD to the other process | |
s = TCPSocket.new 'heroku.com', 80 | |
c.send_io STDOUT | |
c.send_io s | |
s.close # no longer needed here | |
p s.fileno | |
} |
#!/usr/bin/env ruby | |
require 'socket' | |
socket_path = File.expand_path(File.dirname(__FILE__), '.sock') | |
File.delete(socket_path) if File.exist?(socket_path) | |
UNIXServer.open(socket_path) { |serv| | |
s = serv.accept | |
stdout = s.recv_io | |
heroku = TCPSocket.for_fd(s.recv_io.fileno) | |
p stdout.fileno | |
p heroku.fileno | |
p heroku.local_address | |
p heroku.remote_address | |
# Use the socket opened by client that we received above | |
heroku.write "GET / HTTP/1.1\r\n" | |
heroku.write "Host: heroku.com\r\n\r\n" | |
while (line = heroku.gets) | |
break if line.chomp == "0" | |
puts line | |
end | |
heroku.close | |
stdout.puts "done" | |
} |