title: Sending and Receiving arrays via Sockets
body: <p>Is it possible to send an array through UDP Sockets using Python? I am using Python 2.5 and trying to send a simple array but it's not working. It can send the array successfully but when I try to print it with an item of the array the program crashes. I'm not sure what the error is as I take the precaution of converting the data into an array but it's not working. Hope I explained the problem as clearly as possible. I would appreciate the help!</p>
  
  <pre><code># Client program
  
  from socket import *
  import numpy
  from array import*
  
  # Set the socket parameters
  host = "localhost"
  port = 21567
  buf = 4096
  addr = (host,port)
  
  # Create socket
  UDPSock = socket(AF_INET,SOCK_DGRAM)
  
  def_msg = "===Enter message to send to server===";
  print "\n",def_msg
  a = array('i',[1,3,2])
  # Send messages
  while (1):
      data = raw_input('yes or now')
      if data!= "yes":
          break
      else:
          if(UDPSock.sendto(a,addr)):
              print "Sending message"
  
  # Close socket
  UDPSock.close()
  
  
  
  # Server program
  
  from socket import *
  
  # Set the socket parameters
  host = "localhost"
  port = 21567
  buf = 4096
  addr = (host,port)
  
  # Create socket and bind to address
  UDPSock = socket(AF_INET,SOCK_DGRAM)
  UDPSock.bind(addr)
  
  # Receive messages
  while 1:
      data,addr = UDPSock.recvfrom(buf)
      L = eval(data)
      if not data:
          print "Client has exited!"
          break
      else:
          print "\nReceived message '", L[1],"'"
  
  # Close socket
  UDPSock.close()
  </code></pre>