A single UDP port in a multithreaded server
843790Jul 25 2008 — edited Jul 26 2008I'm trying to write a server application that creates a thread for every client. The server/client communication is a combination of TCP and UDP, and I want to use a fixed TCP/UDP port on the server side to make it easier to use behind NAT routers. Here's a summary of what I have done and what I want to achieve:
- The server creates a TCP and UDP channel (I'm using the NIO interface) on the specified ports
- The server waits for incoming clients by calling accept() on the TCP channel
- The server creates a new thread for the new client, and gives the TCP and UDP channels as arguments
- The client informs the server about its UDP port over the TCP connection
- The new server thread connect()s the UDP channel to the IP:port pair received over the TCP connection
I believed that connecting the UDP socket to the IP:port of the client in each thread would make it possible to use a single UDP port for the multithreaded application, but it seems that the connect() call affects the parent thread as well. The next client that tries to connect() gets a "Connect already invoked" error. I tried calling clone() on the UDP channel argument I passed to the new thread, but was not allowed to call clone() because it's protected.
Can someone tell me if what I'm trying to do is possible, and if so, how to achieve it?