Difference between DatagramSocket and DatagramChannel

java, networking, udp

Solution

You're not asking the first question first. The first question is: what messaging discipline is most appropriate for this game?

For a small number of users, UDP is entirely more trouble than it is worth. You've got to worry about lost packets, you've got to come up with some way to package data into small packets, yada, yada, yada.

At the 4-8 player scale, you could interconnect with web services and send soap message around. That takes care of all the data serialization for you. Heck, you could even use JMS.

As for your literal question, channels are part of nio. They support multiplexed-wait, which Sockets do not. If you need to ask 'is there a packet for me on any of these ports?' you want channels. Without them, you need a thread-per-port. Assuming, of course, that you have more than one port on which you are receiving data.

Problem

For this semester in university, we have to write networked games (in java) in teams of 4. I have volunteered to work on the networking code for my team. Reading up on java networking, it seems there are two UDP methods of networking: https://docs.oracle.com/javase/1.5.0/docs/api/java/net/DatagramSocket.html. This is a standard looking UDP socket, which can send packets to any IP address of any port. https://docs.oracle.com/javase/1.5.0/docs/api/java/nio/channels/DatagramChannel.html. This is some sort of channel system, built on top of a udp socket. I'm not entirely sure what it offers, except the ability to only connect to one client, which isn't very useful in this case. Are these the only options? Which is the best to use for a realtime multiplayer game with 4-8 players?

Original source