I've found a lot of general information about running a server using unix sockets, but I can't quite get it working. Here's all I want to do. The server creates a unix socket, then listens on it. When a request comes in, it processes the request, then sends out a response. For now it doesn't matter when the request is or what processing happens. "Hello world" is fine for now.
Here are the server and client scripts I have right now. As far as I can tell, neither works.
The server:
#!/usr/bin/ruby -w
require 'socket'
server = UNIXServer.new('/tmp/socket-simple')
socket = server.accept
socket.write 'Hello world'
socket.close
server.close
The client:
#!/usr/bin/ruby -w
require 'net_http_unix'
request = Net::HTTP::Get.new('whatever')
client = NetX::HTTPUnix.new('unix://' + '/tmp/socket-simple')
response = client.request(request)
puts response.body
What I expect to happen is that the client sends that simple request, gets back "Hello world", and displays that response. Here's what actually happens.
The client script outputs this error:
Traceback (most recent call last):
16: from ./socket-client.rb:8:in `<main>'
15: from /usr/lib/ruby/2.5.0/net/http.rb:1458:in `request'
14: from /usr/lib/ruby/2.5.0/net/http.rb:910:in `start'
13: from /usr/lib/ruby/2.5.0/net/http.rb:1460:in `block in request'
12: from /usr/lib/ruby/2.5.0/net/http.rb:1467:in `request'
11: from /usr/lib/ruby/2.5.0/net/http.rb:1493:in `transport_request'
10: from /usr/lib/ruby/2.5.0/net/http.rb:1536:in `begin_transport'
9: from /var/lib/gems/2.5.0/gems/net_http_unix-0.2.2/lib/net_x/http_unix.rb:24:in `connect'
8: from /var/lib/gems/2.5.0/gems/net_http_unix-0.2.2/lib/net_x/http_unix.rb:35:in `connect_unix'
7: from /usr/lib/ruby/2.5.0/timeout.rb:108:in `timeout'
6: from /usr/lib/ruby/2.5.0/timeout.rb:33:in `catch'
5: from /usr/lib/ruby/2.5.0/timeout.rb:33:in `catch'
4: from /usr/lib/ruby/2.5.0/timeout.rb:33:in `block in catch'
3: from /usr/lib/ruby/2.5.0/timeout.rb:93:in `block in timeout'
2: from /var/lib/gems/2.5.0/gems/net_http_unix-0.2.2/lib/net_x/http_unix.rb:35:in `block in connect_unix'
1: from /var/lib/gems/2.5.0/gems/net_http_unix-0.2.2/lib/net_x/http_unix.rb:35:in `open'
/var/lib/gems/2.5.0/gems/net_http_unix-0.2.2/lib/net_x/http_unix.rb:35:in `initialize': Connection refused - connect(2) for /tmp/socket-simple (Errno::ECONNREFUSED)
The server seems to work OK, except that it leaves the unix socket file open. I can kill that socket by deleting /tmp/socket-simple.
So I'm pretty lost here. Can anybody provide I working examples of client and server scripts that do what I want?