Question

I'm creating a game in Java that simulates the classic 5 cards Poker with 2 to 4 players. Most of the data will be processed by a server, but since I can't use an online server, my idea is to allow a user to host a game by creating a local one.

Now, I don't want to force the use of IPs to connect to a game, so I created a "discovery" interface within the user can see all avaible games. This is done using the UDP protocol and a broadcast research on a common group:

(code is simplified to show only the actions that are executed, may not work as it is showed here)

Client

MulticastSocket socket = new MulticastSocket(6020);
InetAddress group = InetAddress.getByName("226.0.0.1");
socket.joinGroup(group);

DatagramPacket packet = new DatagramPacket(new byte[] {(byte) 0xF0}, 1, group, 6020);
socket.send(packet);

while(true) {
    buf = new byte[1];
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    if(packet.getData()[0] == 15) {
        Socket client = new Socket(packet.getAddress(), 6020);
    }
}

Server

MulticastSocket socket = new MulticastSocket(6020);
InetAddress group = InetAddress.getByName("226.0.0.1");
socket.joinGroup(group);

// new thread listening on port 6020 TCP
ServerSocket server = new ServerSocket(6020);
new Thread(new Runnable() {

            public void run() {
                while(true) {
                    // new thread communicating with client and back listening on port 6020
                    new ServerThread(server.accept());
                }
            }
}).start();

// listening on port 6020 UDP
byte[] buf;
DatagramPacket packet;
while(true) {
    buf = new byte[1];
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    if(packet.getData()[0] == -16) {
        DatagramPacket packet = new DatagramPacket(new byte[] {(byte) 0x0F}, 1, packet.getSocketAddress());
        socket.send(packet);
    }
}

The client sends an UDP broadcast packet on the port 6020. When a server receive this packet if it's composed by a byte 0xF0, he sends back a byte 0x0F to the client. Every client is also listening on the port 6020 and when receive a packet composed by a byte 0x0F it starts a new connection TCP to the server on the port 6020.

My question: is there a better way to achieve this "discovery" system? I know this is going to work only in local networks, is it possible to extend the discovery "outside" using a local server?

Was it helpful?

Solution

Unless you want to set up some sort of known broker that can connect players with servers (or give them a listing of servers), you may be out of luck. As you discovered, multicast and broadcast is generally not sent to the WAN by most switches (and definitely cannot traverse the Internet).

If your issue with setting up a known server/broker is that you have a home connection and so a dynamic ip, I would recommend looking into dynamic DNS. There are a number of providers out there that will allow you to set up a sub-domain on their system that is automatically changed to point to your IP as your IP changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top