title: Python, why it is errors 10035 (on server) and 10053 (on client) during using TCP sockets?
body: <p>Almost 2 days I still have the same problem -client and server 'talks' to each other but I don't know why suddenly problem occurs during the communication. I tried really many things and unfortunately still the same problem. </p>
  
  <p>I'm using python 2.7.5 on Windows 7.</p>
  
  <p>My code:
  <strong>cs_common.py</strong></p>
  
  <pre><code>import socket
  import os
  import sys
  import errno
  from time import sleep
  
  
  HOST = 'localhost'
  MY_IP = socket.gethostbyname(socket.gethostname())
  PORT = 50007
  timeout_in_seconds = 2
  
  def createSocket4server(host, port):
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((host, port))
      s.listen(4)
      return s
  
  def createSocket4Client(host, port, timeout_in_seconds=3):
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
      s.connect( (host, port) )
      print 'connected to %s port %s' % (host, port)
      return s
  
  sent = 0
  def sendToSocket(socket_, data): # to debug
      global sent
      print sent, ') Sending:', data
      socket_.send(data)
      sent += 1
  
  received = 0
  def recvFromSocket(socket_):
      global received
      print '&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive'
      data = socket_.recv(1024)
      print received, ') Received:', data
      received += 1
      return data
  
  def sendDataToAllPlayers(data_to_send, connections):
      for c in connections:
          sendToSocket(c[0], data_to_send)
  
  def trySendingAsLongAsNotSent(socket_, data):
      while True:
              try:
                  sendToSocket(socket_, data)
                  break
              except socket.timeout:
                  pass
  
  def tryReceivingAsLongAsNotReceived(socket_):
      while True:
              try:
                  data = recvFromSocket(socket_)
                  return data
              except socket.timeout:
                  pass
  </code></pre>
  
  <p><strong>server.py:</strong></p>
  
  <pre><code>from cs_common import *
  
  
  server = createSocket4server(HOST, PORT)
  server.setblocking(0) # 1 switch off blocking
  server.settimeout(timeout_in_seconds)
  
  connections = []
  
  counter = 0
  while counter&lt;3:
      counter += 1
      try:
          c, addr = server.accept()
          print 'Got connection from', addr
          connections.append( [c, addr] )
      except socket.timeout:
          pass
  
  if len(connections) == 0:
      print 'No connections!'
      exit()
  
  number_of_players = len(connections)
  print 'Connected with', number_of_players, 'players:'
  print [ addr[1] for addr in connections ]
  
  counter = 0
  for c in connections:
          counter += 1
          number_of_the_player = counter
          initial_game_data = str(number_of_the_player) + ' rest of initial game data'
  
          sendToSocket(c[0], initial_game_data) # 2 sending initial game settings
  
  sleep(1)
  server.setblocking(1) # 3 switch on blocking
  
  # MAIN LOOP #
  while True:
      print 'LOOP___________________________'
      sendDataToAllPlayers('Synchronization data to players', connections) # 4 sending synchronization data to clients
  
      # doing some stuff
  
      for c in connections:
          print 'received from player:', recvFromSocket(c[0]) # 5 receiving synchronization data from clients
  </code></pre>
  
  <p><strong>client.py:</strong></p>
  
  <pre><code>from cs_common import *
  
  
  server = createSocket4Client(HOST, PORT)
  server.setblocking(0) # 1 switch off blocking
  server.settimeout(timeout_in_seconds)
  
  initial_game_data = tryReceivingAsLongAsNotReceived(server) # 2 getting initial game settings
  print 'received initial_game_data from server:', initial_game_data
  
  sleep(1)
  server.setblocking(1) # 3 switch on blocking
  
  # MAIN LOOP #
  while True:
      print 'LOOP___________________________'
      sunchronizing_data = recvFromSocket(server) # 4 receive synchronization data from server
      print 'Received from server:', sunchronizing_data
  
      # doing some stuff
  
      sendToSocket(server, 'I was doing nothing during the frame')
  </code></pre>
  
  <p>When I run the codes above:</p>
  
  <p>Output from client:</p>
  
  <pre><code>connected to localhost port 50007
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  0 ) Received: 1 rest of initial game data
  received initial_game_data from server: 1 rest of initial game data
  LOOP___________________________
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  1 ) Received: Synchronization data to players
  Received from server: Synchronization data to players
  0 ) Sending: I was doing nothing during the frame
  LOOP___________________________
  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  Traceback (most recent call last):
    File ".\client.py", line 19, in &lt;module&gt;
      sunchronizing_data = recvFromSocket(server) # 4 receive synchronization data from server
    File "...\pygame_my\cs_common.py", line 38, in recvFromSocket
      data = socket_.recv(1024)
  socket.error: [Errno 10053] An established connection was aborted by the softwar
  e in your host machine
  </code></pre>
  
  <p>Output from server:</p>
  
  <pre><code>Got connection from ('127.0.0.1', 55768)
  Connected with 1 players:
  [('127.0.0.1', 55768)]
  0 ) Sending: 1 rest of initial game data
  LOOP___________________________
  1 ) Sending: Synchronization data to players
  received from player: &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;Trying to receive
  Traceback (most recent call last):
    File ".\server.py", line 49, in &lt;module&gt;
      print 'received from player:', recvFromSocket(c[0]) # 5 receiving synchronization data from clients
    File "...\pygame_my\cs_common.py", line 38, in recvFromSocket
      data = socket_.recv(1024)
  socket.error: [Errno 10035] A non-blocking socket operation could not be completed immediately
  </code></pre>
  
  <p>I tried also the scripts on an another computer -the same problems. I tried also to deactivate not-blocking sockets -still problems</p>