Search code examples
javanetwork-programmingserverdatagram

How do you connect a DatagramSocket server and client outside of a local area network(LAN)?


I am able to connect my DatagramSocket server and client inside my local area network(LAN) but I am unable outside of it.

Here is my code:

GameServer class:

public class GameServer extends Thread {

private DatagramSocket socket;

public GameServer() {
    try {
        this.socket = new DatagramSocket(7081,InetAddress.getByName("0.0.0.0"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void run() {
    while (true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        try {
            socket.receive(packet);
            if(new String(packet.getData()).trim().equalsIgnoreCase("ping")){
                sendData("pong".getBytes(),packet.getAddress(),packet.getPort());
            }
            System.out.println(new String(packet.getData()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


public void sendData(byte[] data, InetAddress ipAddress, int port) {

        DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port);
        try {
            this.socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

}

GameClient class:

public class GameClient extends Thread {

private InetAddress ipAddress;
private DatagramSocket socket;

public GameClient(String ipAddress) {
    try {
        this.socket = new DatagramSocket();
        this.ipAddress = InetAddress.getByName(ipAddress);
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

public void run() {
    while (true) {
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        try {
            socket.receive(packet);
            System.out.println(new String(packet.getData()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


public void sendData(byte[] data) {

        DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 7081);
        try {
            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
    }
}

}

RunClient class:

public class RunClient {
public static void main(String[] args) {
    GameClient client = new GameClient("/*Here is my external ip address*/"); //If i am running this for LAN then 192.168.1.67 and localhost work
    client.start();
    client.sendData("ping".getBytes());
}
}

RunServer class:

public class RunServer {
public static void main(String[] args) {
    GameServer server = new GameServer();
    server.start();
}
}

Here are my port forwarding configurations:

Here are my port forwarding configurations

Some additional information:
My local ip address is 192.168.1.67
The port I am using is 7081
When I check my ip and port at http://www.canyouseeme.org it gave me the following error:

Error: I could not see your service on "my external ip" on port (7081)

Solution

  • I don't see any problem in your code. That site (http://www.canyouseeme.org) must be trying with TCP instead of UDP.