A PC cannot know its public IP on its own; it needs a third party, that is a STUN server, it’s like a mirror.

This is using the Java programming language and an operating system like Linux.

On the server you can create a /stun directory

You must create a file called StunServer.java

its content is:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class StunServer {

    public static void main(String[] args) {
        int listenPort = 19302; // El puerto en el que el servidor va a escuchar
        try {
            // Se crea un socket UDP para escuchar en un puerto específico
            DatagramSocket serverSocket = new DatagramSocket(listenPort);
            byte[] receiveData = new byte[1024];

            System.out.println("Servidor STUN simple iniciado. Esperando mensajes en el puerto " + listenPort + "...");

            while (true) {
                // 1. Recibe el paquete del cliente
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);

                // 2. Obtiene la IP y el puerto de la máquina que envió el mensaje
                InetAddress clientAddress = receivePacket.getAddress();
                int clientPort = receivePacket.getPort();

                String clientIP = clientAddress.getHostAddress();

                System.out.println("--- Mensaje recibido del cliente ---");
                System.out.println("IP de origen del cliente: " + clientIP);
                System.out.println("Puerto efímero del cliente: " + clientPort);
                System.out.println("------------------------------------");

                // 3. Prepara el mensaje de respuesta con los datos
                String responseData = "Tu IP publica es: " + clientIP + ", y tu puerto efimero es: " + clientPort;
                byte[] sendData = responseData.getBytes();

                // 4. Envía el paquete de respuesta de vuelta al cliente
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);
                serverSocket.send(sendPacket);
                System.out.println("Respuesta enviada automáticamente.");
            }

        } catch (IOException e) {
            System.err.println("Error del servidor: " + e.getMessage());
        }
    }
}

This will show the public IP from which it communicates and the ephemeral port assigned by the router’s NAT system.

also returns a response

PC (asks for its own data) -> STUN server

then:

STUN server (shows PC IP and ephemeral port) (sends response)-> PC (knows what its own public IP and ephemeral port are that it is using)

to compile the Java code (in the same location where the file is)

javac StunServer.java

then StunServer.class is created and you must run it

java StunServer

You should not run java StunServer.class

to run it in the background (i.e. without having a terminal open with the process)

java StunServer &

You must do “port forwarding” on the router, from an external port to the internal port 19302 and to the server’s private IP, in this example I use the external port 50000

Then from another PC with netcat you must consult with UDP to the port where you did “port forwarding”

then from another PC you query the STUN server

PC:

STUN server:

You can also improve the nc command so that it returns to the command line after receiving a response:

printf "Dame mi IP" | nc -u -w 5 www.axxelin.com 50000

-w 5: This is the key flag. It tells netcat to wait up to 5 seconds for something to happen (such as receiving a response) before terminating.