Basic UDP question

I’m attempting to try UDP networking, however the server is not reporting receiving the datagram. I’m not sure what I am doing wrong.

server:

from panda3d.core import QueuedConnectionManager, QueuedConnectionListener, QueuedConnectionReader, ConnectionWriter, PointerToConnection, NetAddress, NetDatagram

from direct.directbase.DirectStart import *

class SN:
    def __init__(self):
        self.cManager = QueuedConnectionManager()
        self.cReader = QueuedConnectionReader(self.cManager, 0)
        self.cWriter = ConnectionWriter(self.cManager,0)
        
        self.port_address=9099 #No-other TCP/IP services are using this port
        self.udpSocket = self.cManager.openUDPConnection(self.port_address)
        
        taskMgr.add(self.tskReaderPolling,"Poll the connection reader",-40)
    
      
    def tskReaderPolling(self, taskdata):
        if self.cReader.dataAvailable():
            datagram=NetDatagram()  # catch the incoming data in this instance
            # Check the return value; if we were threaded, someone else could have
            # snagged this data before we did
            print "I got data!"
            if self.cReader.getData(datagram):
                print "I got one!"
        return taskdata.cont
  
sn = SN()
run()

client:

from panda3d.core import QueuedConnectionManager, QueuedConnectionListener, QueuedConnectionReader, ConnectionWriter, PointerToConnection, NetAddress, NetDatagram
from direct.distributed.PyDatagram import PyDatagram

from direct.directbase.DirectStart import *

port_address=9099  # same for client and server
ip_address="127.0.0.1"
 
cManager = QueuedConnectionManager()
cReader = QueuedConnectionReader(cManager, 0)
cWriter = ConnectionWriter(cManager,0)
 
myConnection=cManager.openUDPConnection()
    
myPyDatagram = PyDatagram()
myPyDatagram.addUint8(1)
myPyDatagram.addString("Hello, world!")

serverAddress = NetAddress()
serverAddress.setHost(ip_address, port_address)

cWriter.send(myPyDatagram, myConnection, serverAddress)

run()

You have to tell your ConnectionReader that it should listen on the UDP socket on the server side:

self.cReader.addConnection(self.udpSocket)

Also, for robustness it might be a good idea to make sure your message actually went out on the client side:

sent = cWriter.send(myPyDatagram, myConnection, serverAddress)
while not sent:
    print "resending"
    sent = cWriter.send(myPyDatagram, myConnection, serverAddress)

Or just pass an extra True parameter to ConnectionWriter.send().

David