Opens a TCP port on the gateway. Return true, if operation was successful
port
- TCP port (0-65535)name
- UPnP rule description (if value is not set, will default name)duration
- duration rule in seconds (0 if is not set)(Read this!)
Opens a UDP port on the gateway. Return true, if operation was successful
port
- UDP port (0-65535)name
- UPnP rule description (if value is not set, will default name)duration
- duration rule in seconds (0 if is not set)(Read this!)
Closes a TCP port on the gateway. Return true, if operation was successful. Most gateways seem to refuse to do this
port
- TCP port (0-65535)
Closes a UDP port on the gateway. Return true, if operation was successful. Most gateways seem to refuse to do this
port
- UDP port (0-65535)
Checks if a TCP port is mapped. Return true if the port is mapped
port
- TCP port (0-65535)
Checks if a UDP port is mapped. Return true if the port is mapped
port
- UDP port (0-65535)
Gets the external IP address of the default gateway. Return external IP address as string, or null if not available
Gets the internal IP address of this machine. Return internal IP address as string, or null if not available
Gets the IP address of the router. Return internal IP address as string, or null if not available
Gets the port mappings of the router. Recommended to use this method with caution due to its labor-intensive nature. Return Set port mappings as PortMappingEntity, or empty if not available
Can run custom upnp command WANIPConnection:1. Return map of strings response, or null, if error
params
- request parameters, Map<String, String>
import com.dosse.upnp.UPnP;
public class Main {
public static void main(String[] args) {
UPnP.openPortTCP(23456);
}
}
import com.dosse.upnp.UPnP;
public class Main {
public static void main(String[] args) {
UPnP.openPortUDP(23456);
}
}
import com.dosse.upnp.UPnP;
public class Main {
public static void main(String[] args) {
UPnP.openPortTCP(23456, "Ultimate port (TCP 23456)", 3 * 60);
}
}
import com.dosse.upnp.UPnP;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
public static void main(String[] args) {
int duration = 180;
int taskPeriod = duration - 10; // -10 - time margin to keep the port open
TimerTask task = new TimerTask() {
public void run() {
UPnP.closePortTCP(23456);
UPnP.openPortTCP(23456, duration);
}
};
new Timer().scheduleAtFixedRate(task, 0, taskPeriod * 1_000L);
}
}
import com.dosse.upnp.UPnP;
public class Main {
public static void main(String[] args) {
int port = 23333;
if (UPnP.isMappedTCP(port)) {
System.out.printf("Port %s is opened%n", port);
} else {
System.out.printf("Port %s is closed or unavailable%n", port);
}
}
}
import com.dosse.upnp.UPnP;
public class Main {
public static void main(String[] args) {
UPnP.getPortMappings().forEach(portMappingEntity -> System.out.println(portMappingEntity.getDescription()));
}
}
Be careful, if you open a port indefinitely and do not close it during the program (or when the program crashes), the port will remain open until you reboot the router/UPnP or close the port from the program