tags:

views:

554

answers:

1

For an UDP server architecture that will have long-lived connections, one architecture is to have one socket that listens to all incoming UDP traffic, and then create separate sockets for each connection using connect() to set the remote address. My question is whether it is possible to do this atomically similar to what accept() does for TCP.

The reason for creating a separate socket and using connect() is that this makes it easy to spread the packet-processing across multiple threads, and also make it easier to have the socket directly associated with the data structures that are needed for processing. The demultiplexing logic in the networking stack will route the incoming packets to the most specific socket.

Now my question is basically what happens when one wants to emulate accept() for UDP like this:

  1. Use select() with a fd-set that includes the UDP server-socket.

  2. Then read a packet from the UDP server-socket.

  3. Then create a new UDP socket which is then connect()ed to the remote address

  4. I call select() with a fd-set that includes both sockets.

  5. What is returned?

given that a packet arrives to the OS somewhere between 1 and 3.

Will the packet be demultiplexed to the UDP server-socket, or will it be demultiplexed to the more specific socket created in 3. That is, at what point does demultiplexing take place? When the packet arrives, or must it happen "as if" it arrived at point 4?

Follow-up question in case the above does not work: What's the best way to do this?

A: 

This will not work.
You have two simple options.

  1. Create a multi-threaded program that has a 'root' thread listening on the UDP socket and 'dispatching' received packets to the correct thread based on the source. This is because you want to segregate processing by source.

  2. Extend your protocol so the the sources accept an incoming connection on some fixed port and then continue with the protocol communication. In this case you would let the source request on the standard UDP port (of your choice), then your end will respond from a new UDP socket to the sources' UDP port. This way you have initiated a new UDP path from your end backwards to the known UDP port of each source. That way you have different UDP sockets at your end.

nik
Why will this not work? See http://lxr.linux.no/linux-bk+v2.6.5/net/ipv4/udp.c#L222for an example of an OS that will demultiplex to the most specific socket.My issue with your proposed solutions is that solution 1 might not scale, and packets generated by solution 2 might not reach clients behind certain NATs.
Ok, I had no idea you were willing to code under the socket layer.
nik