问题
How can I make a port forwarding from a android device to router using the IP from another device?
I want to connect from android device externally to a routers public ip:port so that I can access the hardware device that is connected to the router.(Android -> external IP:Port) -> Router -> hardware Device(hardware device has its own IP and mac).

Code:
PortMapping mapping = new PortMapping();
UnsignedIntegerTwoBytes externalPort = new UnsignedIntegerTwoBytes(22555L);
UnsignedIntegerTwoBytes internalPort = new UnsignedIntegerTwoBytes(80L);
mapping.setDescription("HardwareDescription");
mapping.setEnabled(true);
mapping.setExternalPort(externalPort);
mapping.setInternalClient("192.168.2.68");
mapping.setInternalPort(internalPort);
mapping.setProtocol(PortMapping.Protocol.TCP);
mapping.setRemoteHost("192.168.2.254");
mUpnpService = new MyUpnpServiceImpl(new PortMappingListener(mapping));
mUpnpService.getRouter();
mUpnpService.getControlPoint().search(SCAN_TIMEOUT);
UpnpServiceImpl:
private class MyUpnpServiceImpl extends UpnpServiceImpl {
public MyUpnpServiceImpl(RegistryListener... listeners) {
super(new AndroidUpnpServiceConfiguration(getWifiManager()),
listeners);
}
@Override
public Router getRouter() {
return super.getRouter();
}
@Override
protected Router createRouter(ProtocolFactory protocolFactory,
Registry registry) {
return new AndroidWifiSwitchableRouter(configuration,
protocolFactory, getWifiManager(), getConnectivityManager());
}
}
The code above doesn't crash, but also it doesn't create any port!
Is this possible? If the answer is yes, can you point me in the right direction.
回答1:
Found the answer for this question. The first step is to enable the UPNP option on router, after this step import the library net.sbbi.upnp search for the router (IGD) device and use the method addPortMapping.
Here is an example for anyone that want to open o port on router using any IP, not just from the current device that the app runs.
https://github.com/ManolescuSebastian/Port_Forward_Android
回答2:
You have to connect to your router via a desktop with any explorer (iexplorer, chrome, etc) try to connect to address 192.168.1.1
(the one that is your gateway
, execute ipconfig
from a cmd
window and you'll see a line which says what is your gateway
[the router]), type in the router's user and password and configure it. Depends on the router and model, consult the router's manual. Look for something that says NAT
, Port Forward
or Virtual Server
. You can open a single port or a range of ports, type in the IP address where those ports should be forwarded, in this case the IP of your equipment. If any doubt, search the internet for your router's name and how to open/forward ports.
Good luck.
来源:https://stackoverflow.com/questions/22399994/android-port-forwarding