【发布时间】:2014-03-14 08:44:07
【问题描述】:
如何使用其他设备的 IP 将端口从安卓设备转发到路由器?
我想从 android 设备外部连接到路由器公共 ip:port 以便我可以访问连接到路由器的硬件设备。(Android -> 外部 IP:Port) -> 路由器 -> 硬件设备(硬件设备有自己的IP和mac)。
代码:
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());
}
}
上面的代码没有崩溃,也没有创建任何端口!
这可能吗? 如果答案是肯定的,你能指出我正确的方向吗?
【问题讨论】:
-
如果你想奖励积分,你需要在这个问题上设置一个赏金。
-
还不清楚您要达到的目标。你想通过网关从外部访问安卓设备吗?
-
第一条评论:不幸的是,这个问题在 2 天内有资格获得赏金 :( 第二条评论:我想从 android 设备外部连接到路由器公共 ip:port 以便我可以访问网关设备连接到路由器。(Android->外部IP:端口)->路由器->网关设备(网关设备有自己的IP和mac)。
-
所以,如果我对您的理解正确...您的 android 在您的网络之外,并且您想连接到路由器后面的网关...那么您无法进行 upnp 转发从安卓设备。这必须来自网络内部,而不是外部。
-
让我们看看我是否得到它。你有一个路由器和一个连接到路由器的设备,它有一个开放的 IP/端口,你想通过你的公共路由器 IP 从 Android 设备访问该设备。如果是这种情况,您只需在路由器中进行 NAT,请配置路由器,以便外部(互联网)连接到该端口的任何人都会自动将请求转发到该 IP/端口。
标签: java android sockets router