Jarek
Jarek

Reputation: 55

Ruby webserver without opened port

I am looking for possibility to have ruby-based webserver communicating by pipe, not by TCP/IP. So I will send HTTP request by pipe and I want to read response by pipe. It should be used as bundled/internal webserver (RPC or something) for desktop application. I don't want to handle ports configuration when there will be more instances of my application running on same machine.

Any ideas?

Thank you in advance.

Upvotes: 1

Views: 244

Answers (4)

kingryan
kingryan

Reputation: 26

Thin supports unix sockets.

Upvotes: 0

rampion
rampion

Reputation: 89053

Try a UNIXSocket You use a local path to specify where the socket connection is, not a port, and you can easily handle multiple simultaneous connections.

# server.rb
require 'socket'
File.delete( filename ) if File.exists? filename
server = UNIXServer.open( filename )
server.listen( queuesize )

puts "waiting on client connection"
while client= server.accept
  puts "got client connection #{client.inspect}"

  child_pid = fork do 
    puts "Asking the client what they want"
    client.puts "Welcome to your server, what can I get for you?"
    until client.eof?
      line = client.gets
      puts "The client wants #{line.chomp.inspect}"
    end
    client.close
  end

  puts "running server (#{child_pid})"
  client.close
  Process.detach(child_pid)
end

server.close


# client.rb
require 'socket'
puts "requesting server connection"
server = UNIXSocket.new( filename )
puts "got server connection #{server}"
line = server.gets
puts "The server said: #{line.chomp.inspect}"
%w{ a-pony a-puppy a-kitten a-million-dollars }.each do |item|
  server.puts item
end
server.close

Upvotes: 2

vartec
vartec

Reputation: 134581

Pipe is for one-way communication, so there is no way you can set up webserver on that. You might try with unix socket. But really simplest solution is to use loopback (127.0.0.1). It's highly optimized, so the speed won't be a problem.

Upvotes: 1

Kibbee
Kibbee

Reputation: 66112

Not an answer to your question. However, if you do end up having to use a TCP/IP HTTP Server, you should ensure that it's only listening on 127.0.0.1. Listening on the local host address should be quite fast, as it won't hit the network, and will also make it a tad more secure to stop people connecting from the outside.

Upvotes: 0

Related Questions