title: Running a python threaded TCP server in the background forever
body: <p>Being rather new to socket programming and threading in general, my issue might be stemming from a misunderstanding of how they work. I am trying to create something that acts as both a client and a server using threading.</p>
  
  <p>Following:
  <a href="https://docs.python.org/3/library/socketserver.html#asynchronous-mixins" rel="nofollow noreferrer">https://docs.python.org/3/library/socketserver.html#asynchronous-mixins</a></p>
  
  <p>I created a client class to go with the server and executed both from a main class. The server supposedly launches normally and doesn't give any errors but when I try to connect from the client, it fails on the following:</p>
  
  <pre><code>    # In client connection
      sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      sock.connect((ip, port))
  </code></pre>
  
  <p>the error I am getting is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./&lt;project&gt;", line 61, in &lt;module&gt;
      client.start_client(serverInfo)
    File "/home/&lt;username&gt;/Documents/Github/project/client.py", line 54, in &lt;startclient&gt;
      &lt;connectionMethod&gt;(cmd)
    File "/home/&lt;username&gt;/Documents/Github/project/client.py", line 112, in &lt;connectionMethod&gt;
      sock.connect((remoteHOST,remotePORT))
      ConnectionRefusedError: [Errno 111] Connection refused
  </code></pre>
  
  <p>Even when I modify the server from the code in the referenced python page (just to run on a specific port, 1234), and try to connect to that port with </p>
  
  <pre><code>with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
      sock.connect(('127.0.0.1',1234))
      sock.sendall('Test message')
  </code></pre>
  
  <p>I get the same problem</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  ConnectionRefusedError: [Errno 111] Connection refused
  </code></pre>
  
  <p>Why is the server refusing connections? No firewall rules or iptables are in place, running the example that has the client and socket together as is from the site works but even removing the <code>server.shutdown()</code> line it still kills itself immediately.</p>
  
  <p>Am I missing something?</p>
  
  <p>The behaviour I am expecting is:</p>
  
  <pre><code>./programA
  &lt;server starts on port 30000&gt;
  </code></pre>
  
  <h1>    </h1>
  
  <pre><code>./programB
  &lt;server starts on port 30001&gt;
  &lt;client starts&gt;
  </code></pre>
  
  <p>--- input/output ---</p>
  
  <p>FROM CLIENT A:</p>
  
  <pre><code>/connect 127.0.0.1 30001
  CONNECTED TO 127.0.0.1 30001
  </code></pre>
  
  <p>ON CLIENT B:</p>
  
  <pre><code>CONNECTION FROM 127.0.0.1 30000
  </code></pre>
  
  <p>Basically once the clients connect the first time they can communicate with each other by typing into a prompt which just creates another socket targeting the 'remote' IP and PORT to send off the message (instant messaging). The sockets are afterwords left to close because they aren't actually responsible for receiving anything back.</p>
  
  <p>I would appreciate any help I can get on this.</p>