title: Errno 111 Connection refused in Python sockets programming
body: <p>I have two python scripts: one server.py and one worker.py
  This is supposed to work like this:</p>
  
  <pre><code>- server.py listens on 5005
  - workers are started and bind to random ports
  - they send a message to the server with their port nr
  - server adds the port to its list of known workers
  - it does this for each worker
  </code></pre>
  
  <p>The problem I have is that after the first worker gets added and I start another one, I get this error on the server side:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "server.py", line 42, in listener
      if handle_join(data.split(',')[1:]) == 1:
    File "server.py", line 122, in handle_join
      s.connect(("",int(worker_ip_port[1])))
    File "/usr/lib/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
  error: [Errno 111] Connection refused
  </code></pre>
  
  <p>The application has grown quite a bit, so I will only include the relevant parts here:
  server.py:</p>
  
  <pre><code>def handle_join(worker_ip_port):
  # e.g. worker_ip_port = 127.0.0.1,55256
  # 
  #
      worker_ip_port = tuple(worker_ip_port)
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
          print "Trying to connect to {}".format(worker_ip_port)
          s.connect(("",int(worker_ip_port[1])))
      if worker_ip_port not in WORKERS:
          WORKERS.append(worker_ip_port)
          print '\t\tNew worker added on {}'.format(worker_ip_port)
          print '\t\tWORKERS: {}'.format(WORKERS)
                  s.send('0'+SIGEND)
                  s.close()
                  return 0
      print '\t\tThat port is already in the worker list'
      s.send('1'+SIGEND)
      s.close()
      return 1
  </code></pre>
  
  <p>worker.py</p>
  
  <pre><code>def read_socket():
  # binds socket, keeps listening
  # once connection is accepted, loops over the socket buffer until the
  # signal for transmission end
  # then returns the data
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     s.bind(("",WORKER_PORT))
     print "listening on {}".format(WORKER_PORT)
     while True:
             buffer = ''
             data = True
             s.listen(0)
             conn, addr = s.accept()
             print "accepted connection"
             while data:
                      data = conn.recv(BUFFER_SIZE)
                      # if the SIGNAL for end of packet is found in current packet
                      # add only up to that part
                      # close socket
                      # return data
                      if data.find(SIGEND) != -1:
                              buffer += data[:data.rfind(SIGEND)]
                              conn.close()
                              s.close()
                              return buffer
                      else:
                              buffer += data
  </code></pre>
  
  <p>NOTES: I've tried both Linux and Windows.
  I've read through all the other suggestion on the site and what I could find on Google. None of the seemed to work.</p>
  
  <p>Tried telnet to the connection established by the second worker:</p>
  
  <pre><code>Trying ::1...
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  </code></pre>
  
  <p>From what I could tell the SECOND worker doesn't get any connection from the server.</p>
  
  <p>The output from netstat was also interesting:</p>
  
  <pre><code>tcp        0      0 0.0.0.0:55271           0.0.0.0:*               LISTEN
  tcp        0      0 127.0.0.1:5005          127.0.0.1:55271         TIME_WAIT
  </code></pre>
  
  <p>55271 is the port the SECOND worker binds to;
  the connection below is the one it uses to send the "I AM HERE" message to the server;</p>
  
  <pre><code>tcp        0      0 0.0.0.0:55269           0.0.0.0:*               LISTEN
  tcp        0      0 127.0.0.1:59240         127.0.0.1:55269         TIME_WAIT
  tcp        0      0 127.0.0.1:5005          127.0.0.1:55269         TIME_WAIT
  </code></pre>
  
  <p>In the case of the FIRST worker (on 55269 here), I can see that there are two connections. As it should be. The first from the worker to the server, with the "I AM HERE". And the second from the server to the worker, sending the confirmation that all is well and that it is now on its workers list.</p>
  
  <p>I am using the multiprocessing framework. Would that be a problem? </p>