UDP Server & Client on same machine

Is there anyway to bind a port to one process and send data to a different port? Currently, I have a server and client process that communicate on the same port through UDP. This works fine when the processes are on different machines, but now that I no longer have a lab to use for testing, I’d like to be able to run the server and the client on the same machine, without altering how they communicate (that is, continue to create datagrams and send them with UDP). This doesn’t work without altering anything, because once the server binds to the port, the client cannot.

What I’m looking for is something like this. The server binds to port 1 and the client binds to port 2. The server listens for traffic on port 1 but sends traffic to port 2, while the client listens on port 2 and sends to port 1. (If other clients join, they would do the same as the first client) Is it possible to specify the port of the destination?

Edit:

Upon further investigation it looks like it is. I’ll keep this thread incase others have the same question and post the solution.

Yep. Totally works. Here you go:

Server:

PORT = 2000
# Start the UDP Connection
self.cManager = QueuedConnectionManager()
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager,0)
self.conn = self.cManager.openUDPConnection(PORT)
self.cReader.addConnection(self.conn)

# Create a task for listening, send data normally

Client:

PORT = 2001
# Start the UDP Connection
self.cManager = QueuedConnectionManager()
self.cReader = QueuedConnectionReader(self.cManager, 0)
self.cWriter = ConnectionWriter(self.cManager,0)
self.conn = self.cManager.openUDPConnection(PORT)
self.cReader.addConnection(self.conn)

# Create a task for listening

# To send data, create a NetAddress for the server, use the local host as 
# the ip, and the port that the server is bound to
self.serverAddr = NetAddress()
self.serverAddr.setHost('127.0.0.1', 2000)

# Create datagram & send it
self.cWriter.send(datagram, self.conn, self.serverAddr)

Hey look at that. It’s a good thing I saved this because I googled it again and found my old answer!