title: Python - error: [Errno 111] Connection refused and error: [Errno 98] Address already in use
body: <p>I'm in the process of learning how to use threads and socket connections.
  I've found literature and examples online on how threads and socket connections work.  Now I'm trying to merge the two together to create a simple send/receive application.  So here is what I put together as an example: </p>
  
  <p>main.py,</p>
  
  <pre><code>import server
  import client
  import threading
  
  def main():
      try:
          threadA = threading.Thread(target=server.serv)
          threadA.start()
  
          threadB = threading.Thread(target=client.client)
          threadB.start()
      except:
          print "Got an Error"
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <blockquote>
    <p>server.py</p>
  </blockquote>
  
  <pre><code>#!/usr/bin/python           # This is server.py file
  
  import socket               # Import socket module
  
  def serv():
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)         # Create a socket object
  host = socket.gethostname() # Get local machine name
  port = 12920                # Reserve a port for your service.
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  
  s.bind((host, port))        # Bind to the port
  
  s.listen(5)                 # Now wait for client connection.
  #while True:
  c, addr = s.accept()     # Establish connection with client.
  print 'Got connection from', addr
  c.send('Thank you for connecting')
  s.shutdown(1)
  c.close()                # Close the connection
  </code></pre>
  
  <blockquote>
    <p>client.py</p>
  </blockquote>
  
  <pre><code>#!/usr/bin/python           # This is client.py file
  
  import socket               # Import socket module
  
  def client():
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)         # Create a socket object
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      host = socket.gethostname() # Get local machine name
      port = 12920                # Reserve a port for your service.
  
      s.connect((host, port))
      print s.recv(1024)
      s.shutdown(1)
      s.close                     # Close the socket when done
  </code></pre>
  
  <p>I able to run the application for the first time without any errors.  Here is the output:
  Got connection from ('172.17.132.98', 50759)<br>
  Thank you for connecting  </p>
  
  <p>When I run the application again (for the second time consecutive), I receive the following error:</p>
  
  <pre><code>Traceback (most recent call last):                                                                                                                                 
  File "/usr/lib64/python2.7/threading.py", line 813, in __bootstrap_inner                                                                                         
         self.run()                                                                                                                                                     
  File "/usr/lib64/python2.7/threading.py", line 766, in run                                                                                                       
      self.__target(*self.__args, **self.__kwargs)                                                                                                                   
  File "/home/cg/root/client.py", line 12, in client                                                                                                               
      s.connect((host, port))                                                                                                                                        
  File "/usr/lib64/python2.7/socket.py", line 228, in meth                                                                                                         
      return getattr(self._sock,name)(*args)                                                                                                                         
  error: [Errno 111] Connection refused  
  </code></pre>
  
  <p>Here I hit the ctrl+Z key to halt the hung application.</p>
  
  <p>And if I try to run the application again, I receive the following error:
  Exception in thread Thread-1:                                                                                                                                      </p>
  
  <pre><code>Traceback (most recent call last):                                                                                                                                 
    File "/usr/lib64/python2.7/threading.py", line 813, in __bootstrap_inner                                                                                         
      self.run()                                                                                                                                                     
    File "/usr/lib64/python2.7/threading.py", line 766, in run                                                                                                       
      self.__target(*self.__args, **self.__kwargs)                                                                                                                   
    File "/home/cg/root/server.py", line 11, in serv                                                                                                                 
      s.bind((host, port))        # Bind to the port                                                                                                                 
    File "/usr/lib64/python2.7/socket.py", line 228, in meth                                                                                                         
      return getattr(self._sock,name)(*args)                                                                                                                         
  error: [Errno 98] Address already in use
  </code></pre>
  
  <p>Can someone tell me what I'm doing wrong?</p>
  
  <p>Thanks InAdvance</p>