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?
Aleksei Matiushkin :
I am unsure what net_http_unix is and why would you bring 3rd-party solutions when everything to accomplish a task is there in ruby core.\n\nSocket ruby documentation. Here is a copy-paste from there.\n\n# echo server\nSocket.unix_server_loop(\"/tmp/sock\") do |sock, _client|\n begin \n IO.copy_stream(sock, sock) \n ensure \n sock.close \n end \nend\n\n# client (another terminal)\nSocket.unix(\"/tmp/sock\") do |sock|\n t = Thread.new { IO.copy_stream(sock, STDOUT) } \n IO.copy_stream(STDIN, sock) \n t.join \nend\n\n\nIn the second terminal you might type anything and it will be echoed. Also you might put debug output to server block to track the interchange.",
2020-01-06T07:17:17