1

RPi3, Raspian Jessie, Python 2.7. Hello all, I am struggling trying to do an IPC using Unix Domain Sockets. I had it working some months ago, but alas, I can't seem to put my hands on those images with working code. My client is getting "errno 111: Connection refused" when it tries to connect to a socket created by another Python program. I am currently using some demo code I found here, and I believe it to be OK. Here's the code:

Socket Creator:

import socket
import os, os.path
import time
if os.path.exists( "/tmp/python_unix_sockets_example" ):
  os.remove( "/tmp/python_unix_sockets_example" )
print "Opening socket..."
server = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
server.bind("/tmp/python_unix_sockets_example")

print "Listening..."
while True:
  datagram = server.recv( 1024 )
  if not datagram:
    break
  else:
    print "-" * 20
    print datagram
    if "DONE" == datagram:
      break
print "-" * 20
print "Shutting down..."
server.close()
os.remove( "/tmp/python_unix_sockets_example" )
print "Done"

Socket Connector:

import socket
import os, os.path
print "Connecting..."
if os.path.exists( "/tmp/python_unix_sockets_example" ):
  client = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
  client.connect( "/tmp/python_unix_sockets_example" )
  print "Ready."
  print "Ctrl-C to quit."
  print "Sending 'DONE' shuts down the server and quits."
  while True:
    try:
      x = raw_input( "> " )
      if "" != x:
        print "SEND:", x
        client.send( x )
        if "DONE" == x:
          print "Shutting down."
          break
    except KeyboardInterrupt, k:
      print "Shutting down."
  client.close()
else:
  print "Couldn't Connect!"
print "Done"

ls -l of socket:

srwxr-xr-x 1 root root      0 May 19 14:26 python_unix_sockets_example

I'm thinking this is some stupid, simple thing, but I've been pounding my head against it for a couple days now. Thanks for all help!

20160520@1413EDT update: Still fighting. Changed from Unix Domain Sockets to regular IP sockets (socket.AF_INET), pointed both creator and client to "localhost", ran out of two separate terminal sessions on the same RPi3 and the client finally connects. Now, let me try to work backwards to local domain sockets and see if a miracle will occur...Stay tuned.

  • Add the ls -l output of the socket to your question. – ott-- May 19 '16 at 18:05
  • @ott, See my edit to the original post. – AutoDoc May 19 '16 at 18:41
  • HI is it because when you use bind() you need to specify the port and the host? for Instance server.bind((host, Port)) also I really also think that when using UDP You need to use bind() at the client end too. – Shan-Desai May 19 '16 at 18:47
  • @Shan-Desai Yes, if I was using network sockets, bit this is a Unix Domain Socket. Both sides of the socket exist on the same machine. Is that right? – AutoDoc May 19 '16 at 18:52
  • @AutoDoc Hi Can you do something like resuse the Socket. you can tell the socket to be resused if you are using Loopback Address. socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) this makes the multiuse of socket. – Shan-Desai May 19 '16 at 18:58
1

Finally: The reason I was unable to connect both ends to the domain socket was because (for an unknown reason to me) I was running my logic out of Idle. If I fired up two different terminal sessions; one with the creator, and the other with the client, all worked as expected. There's three days of my life that I won't get back (unless I forget how I got things going and go back through this again).

Thanks to @ott and @Shan-Desai for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.