title: Send/receive data through socket using python and ctype Structure
body: <p>I am trying to send and receive C structure data from a python client to server and vice-versa. Both clients and server work smoothly in connecting and exchanging data. Client sends a ctype structure to server and server sends it back again. The problem is that I don't know how to interpret the message received in the client and extract the data as it is in the structure format. My final Idea is to have a python server and C-client talking to each other and exchanging data in a predefined structure.</p>
  
  <p>Here are the codes I have for client and server in Python:</p>
  
  <p><strong>Client Code</strong></p>
  
  <pre><code>import socket
  import sys
  import time
  
  from ctypes import *
  
  class payload_t(Structure):
      _fields_ = [("ms", c_ulong),
                  ("counter", c_ulong),
                  ("DHT_temperature", c_float),
                  ("DHT_humidity", c_float),
                  ("DS_temperature", c_float),
                  ("temperature_setpoint", c_float),
                  ("time_setpoint", c_float)]
  
  # Create a TCP/IP socket
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  # Connect the socket to the port where the server is listening
  server_address = ('localhost', 10000)
  print &gt;&gt;sys.stderr, 'connecting to %s port %s' % server_address
  sock.connect(server_address)
  
  try:
      for i in range(0,10):
          # Send data
          payload=payload_t(i*1000,i+1,25.2,45.7,25.8,33.22,3600.0)
          message = payload
          # 'This is the message.  It will be repeated.'
          print 'length of message %d' % sizeof(message)
          print 'sending "', message.ms, message.counter, message.DHT_temperature, message.DHT_humidity, message.DS_temperature, message.temperature_setpoint, message.time_setpoint, '"'
          sock.sendall(message)
          # time.sleep(0.1)
          # Look for the response
          amount_received = 0
          amount_expected = sizeof(message)
  
          while amount_received &lt; amount_expected:
              datap = sock.recv(sizeof(message))
              amount_received += len(datap)
          print &gt;&gt;sys.stderr, 'received "%s"' % datap
          print type(datap)
          payload=payload_t()
          datap.readinto(payload)
          data=datap.readinto(payload_t)
          data=struct.unpack(payload_t,datap)
              print 'Received "', data.ms, data.counter, data.DHT_temperature, data.DHT_humidity, data.DS_temperature, data.temperature_setpoint, data.time_setpoint, '"'
  
  finally:
      print &gt;&gt;sys.stderr, 'closing socket'
      sock.close()
  </code></pre>
  
  <p><strong>Server Code</strong></p>
  
  <pre><code>import socket
  import sys
  from ctypes import *
  
  class payload_t(Structure):
      _fields_ = [("ms", c_ulong),
                  ("counter", c_ulong),
                  ("DHT_temperature", c_float),
                  ("DHT_humidity", c_float),
                  ("DS_temperature", c_float),
                  ("temperature_setpoint", c_float),
                  ("time_setpoint", c_float)]
  
  # Create a TCP/IP socket
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  # Bind the socket to the port
  server_address = ('localhost', 10000)
  print &gt;&gt;sys.stderr, 'starting up on %s port %s' % server_address
  sock.bind(server_address)
  
  # Listen for incoming connections
  sock.listen(1)
  payload=payload_t(0,0,0.0,0.0,0.0,0.0,0.0)
  while True:
      # Wait for a connection
      print &gt;&gt;sys.stderr, 'waiting for a connection'
      connection, client_address = sock.accept()
  
      try:
          print &gt;&gt;sys.stderr, 'connection from', client_address
  
          # Receive the data in small chunks and retransmit it
          while True:
              data = connection.recv(sizeof(payload))
              print &gt;&gt;sys.stderr, 'received "%s"' % data
              if data:
                  print &gt;&gt;sys.stderr, 'sending data back to the client'
                  connection.sendall(data)
              else:
                  print &gt;&gt;sys.stderr, 'no more data from', client_address
                  break
  
      finally:
          # Clean up the connection
          connection.close()
  </code></pre>