import socket, thread, time
from System.Collections.Generic import *
 
def myUdpServer(UDP_PORT):
    UDP_IP = "" #listen on all IPs    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((UDP_IP, UDP_PORT))
    listening = True
    print "UDP ready, using port:"
    print UDP_PORT
    
    while (listening):
        
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print data
        #this would be a good place to check if the message should be used or discarded
        #or to tell the server to stop etc.
        
        #create a single event for any message received and send the data as payload 1
        vc.triggerEvent("UDPIP", List[str]([data]))
        
        #or you could create a unique eventname for each data if you want to use multiple commands
        #vc.triggerEvent("UDPIP."+data, None)
        
    #exit the loop    
    print "UDP closed"
    sock.close()
    sock = null

thread.start_new_thread( myUdpServer, (65432,) )
