title: Socket error - with simple socket programming using python
body: <p>There is a server process listening in my machine at port 8000. And, a client process is trying to communicate to the server continuously. Everytime it opens a new socket connection and closes the connection for some reason. This is a continuously process. But, when I'm trying to do with the pasted code here, I'm unable to create the socket connection after certain limit. In my windows machine, it throws error after creating/closing 16k connections. </p>
  
  <p>Getting an 10055 error while running the example code given here. "Connect failed. Error Code : 10055 Message An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full"</p>
  
  <pre><code>import socket              
  import sys
  from time import sleep
  
  
  HOST = socket.gethostbyname(socket.gethostname())
  PORT = 8000               
  open = 0
  close = 0
  
  def connect(host, port, message=None):
      global open
      global close
      open += 1
  
      s = socket.socket()
  #     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      print 'Socket created'
      try:
          s.connect((host, port))
      except socket.error , msg:
          print "open count=%s" % open
          print "close count=%s" % close
          print 'Connect failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
          sys.exit()
      print "Sending a msg"
  
      s.sendall('Hello\n') #send only takes string
  
      count = 1
      while True:
          data = s.recv(1024)
          print "RECE data:&lt;%s&gt;" % data
          if not data and count &lt; 5:
              print "Wait for data..." 
              count += 1
          else:
              break
  
      s.shutdown(1)
      s.close()
      del s
      close += 1
      print 'Socket connect complete for %s:%s' % (host, port)
  
  while 1:
      print 'Connected with ',  HOST
      connect(HOST, 8000)
      print "Wait for 1 sec"
      sleep(1)
  
  output like this:
  &gt;&gt;&gt;
  :
  :
  Wait for 1 sec
  Connected with  &lt;server-ip&gt;
  Socket created
  Sending a msg
  RECE data:&lt;Got your msg: &lt;Hello&gt;
  &gt;
  Socket connect complete for &lt;server-ip:port&gt;
  
  Wait for 1 sec
  Socket created &lt;server-ip&gt;
  ### open count=16338
  ### close count=16337
  Connect failed. Error Code : 10055 Message An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full
  &lt;&lt;&lt;
  </code></pre>