title: Why is host aborting connection?
body: <p>I'm teaching myself Python networking, and I recalled that back when I was teaching myself threading, I came across <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/" rel="noreferrer">this page</a>, so I copied the scripts, updated them for Python 3.1.1 and ran them.  They worked perfectly.</p>
  
  <p>Then I made a few modifications.  My goal is to do something simple:</p>
  
  <ol>
  <li>The client pickles an integer and sends it to the server.</li>
  <li>The server receives the pickled integer, unpickles it, doubles it, then pickles it and sends it back to the client.</li>
  <li>The client receives the pickled (and doubled) integer, unpickles it, and outputs it.</li>
  </ol>
  
  <p>Here's the server:</p>
  
  <pre><code>import pickle
  import socket
  import threading
  
  class ClientThread(threading.Thread):
      def __init__(self, channel, details):
          self.channel = channel
          self.details = details
          threading.Thread.__init__ ( self )
  
      def run(self):
          print('Received connection:', self.details[0])
          request = self.channel.recv(1024)
          response = pickle.dumps(pickle.loads(request) * 2)
          self.channel.send(response)
          self.channel.close()
          print('Closed connection:', self.details [ 0 ])
  
  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server.bind(('', 2727))
  server.listen(5)
  
  while True:
      channel, details = server.accept()
      ClientThread(channel, details).start()
  </code></pre>
  
  <p>And here is the client:</p>
  
  <pre><code>import pickle
  import socket
  import threading
  
  class ConnectionThread(threading.Thread):
      def run(self):
          client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          client.connect(('localhost', 2727))
  
          for x in range(10):
              client.send(pickle.dumps(x))
              print('Sent:',str(x))
              print('Received:',repr(pickle.loads(client.recv(1024))))
  
          client.close()
  
  for x in range(5):
      ConnectionThread().start()
  </code></pre>
  
  <p>The server runs fine, and when I run the client it successfully connects and starts sending integers and receiving them back doubled as expected.  However, very quickly it exceptions out:</p>
  
  <pre><code>Exception in thread Thread-2:
  Traceback (most recent call last):
    File "C:\Python30\lib\threading.py", line 507, in _bootstrap_inner
      self.run()
    File "C:\Users\Imagist\Desktop\server\client.py", line 13, in run
      print('Received:',repr(pickle.loads(client.recv(1024))))
  socket.error: [Errno 10053] An established connection was aborted by the softwar
  e in your host machine
  </code></pre>
  
  <p>The server continues to run and receives connections just fine; only the client crashes.  What's causing this?</p>
  
  <p>EDIT: I got the client working with the following code:</p>
  
  <pre><code>import pickle
  import socket
  import threading
  
  class ConnectionThread(threading.Thread):
      def run(self):
          for x in range(10):
              client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              client.connect(('localhost', 2727))
              client.send(pickle.dumps(x))
              print('Sent:',str(x))
              print('Received:',repr(pickle.loads(client.recv(1024))))
              client.close()
  
  for x in range(5):
      ConnectionThread().start()
  </code></pre>
  
  <p>However, I still don't understand what's going on.  Isn't this just opening and closing the socket a bunch of times?  Shouldn't there be time limitations to that (you shouldn't be able to open a socket so soon after closing it)?</p>