Android adb 将远程端口转发到本地机器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3415797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:05:54  来源:igfitidea点击:

adb forward remote port to local machine

androidportforwardingadb

提问by Gaurav Khurana

This is a query regarding the usage of adb on android.

这是一个关于在an​​droid上使用adb的查询。

Is there a way to forward the remote port i.e. port on the android device/emulator to the local machine to which the device is connected?

有没有办法将android设备/模拟器上的远程端口即端口转发到设备所连接的本地机器?

$ adb forward tcp:port1 tcp:port2 # forwards the local port port1 on the machine to port2 on the device.

$ adb forward tcp:port1 tcp:port2 # 将机器上的本地端口port1 转发到设备上的port2。

Thanks in advance!

提前致谢!

回答by molokoka

Recently google updated ADB service. And added reversecommand that should do the job.

最近谷歌更新了亚行服务。并添加reverse了应该完成这项工作的命令。

From reversedocumentation:

反向文档:

This implements the adb reversefeature, i.e. the ability to reverse socket connections from a device to the host. <forward-command>is one of the forwarding commands that are described above, as in:

  1. list-forward
  2. forward:<local>;<remote>
  3. forward:norebind:<local>;<remote>
  4. killforward-all
  5. killforward:<local>

Note that in this case, <local>corresponds to the socket on the device and <remote>corresponds to the socket on the host.

The output of reverse:list-forwardis the same as host:list-forward except that <serial>will be just host.

这实现了该adb reverse功能,即反向从设备到主机的套接字连接的能力。<forward-command>是上面描述的转发命令之一,如:

  1. list-forward
  2. forward:<local>;<remote>
  3. forward:norebind:<local>;<remote>
  4. killforward-all
  5. killforward:<local>

注意,在这种情况下,<local>对应于设备<remote>上的套接字,对应于主机上的套接字。

的输出reverse:list-forward与 host:list-forward 相同,<serial>只是host.

回答by RickNotFred

Do you truly need to forward the port or are you just looking for a way to communicate using sockets from a program running on the android shell to your host machine? I was able to accomplish the latter by sending my message to the port of interest on "10.0.2.2", which is the loopback adapter of the host machine. See docs here.

您是否真的需要转发端口,或者您只是在寻找一种使用套接字从运行在 android shell 上的程序到您的主机的通信方式?我能够通过将我的消息发送到“10.0.2.2”上感兴趣的端口来完成后者,这是主机的环回适配器。请参阅此处的文档。

EDIT
This is the updated link

编辑
这是更新的链接