Multiple DatagramChannels

Hi,

I was thinking of using one DatagramChannel per client instead of using only one DatagramChannel for all the clients. The main reason I want to do this is so that I can connect the DatagramChannel and then use the write(ByteBuffer[]) so that I can use seperate packets for the header and body of my messages. (Stupidly, Sun did not provide a send(ByteBuffer[], address) method for gathering sends)

Is there any problem with this method?

Thanks,
Sam

Each send (as opposed to write) incurs extra overhead. That’s why they only have a write version of gathered reads - if you want the efficiency of gathering, you want the efficiency of a connected DC.

At least, I believe that’s how the theory goes.

Then again, Sun’s NIO team are crap at gathering and scattering and if you’re using java 1.4.x it’s not worth bothering - you can actually write it FASTER in your own user-level SOFTWARE than their supposed invoking of low-level routines from their own JVM. That’s pretty damn crap. Java 5? Haven’t tested it yet…

Extra overhead with sends! I remember reading that in the javadoc but I did not really beleive it… So, it sounds like creating connected DatagramChannels for each client can’t hurt (too much).

If I have an unconnected DatagramChannel (used to receive new “connections”) and a DatagramChannel connected to a client, when the client sends a packet which channel will be marked as ready to read?

The main reason I want to use gathering writes is so that I don’t have to create a new ByteBuffer when I want to append a header onto a packet.

-Sam

<Threadjack (ish)>

What about binding the multiple sockets to the same port at the server end using SO_REUSEADDR ?

Would that work?

</Threadjack (ish)>

Cheers

Endolf