Is it possible to send an array via UDP sockets using Python? I am using Python 2.5 and trying to send a simple array, but it does not work. It can send an array successfully, but when I try to print it with an array element, the program will work. I am not sure what the error is, as I take precautions when converting data to an array, but it does not work. I hope I explained the problem as clearly as possible. I would appreciate help!
# 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()
python udp sockets
dawnoflife
source share