我可以从一个接口端口转发到另一个接口端口吗? [英] Can I port forward from one interface port to another interface port?

查看:408
本文介绍了我可以从一个接口端口转发到另一个接口端口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编程TCP套接字",并且有一些基本问题.

I am "programming TCP sockets" and have some fundamental questions.

当我为TCP客户端指定IP地址和端口时,我知道IP地址属于网络接口,而不是实际的整体计算机,并且该计算机具有多个网络接口,包括向内的本地接口.主机回送接口.

When I specify an IP address and port for a TCP client, I know that the IP address belongs to a network interface and not the actual computer as a whole, and that the computer has multiple network interfaces, including the inward facing local host loop back interface.

指定给TCP客户端的端口是否属于指定的接口?例如,是否可以在两个不同的接口上有两个相同编号的端口?像接口A的端口5000和接口的端口5000环回一样?

Does the port specified to the TCP client belong to the specified interface? Like for example, can there be two ports of the same number but on two different interfaces? Like port 5000 of interface A and port 5000 of interface loop back?

我问这个问题是因为我试图创建一个TCP客户端,该客户端将通过套接字连接到在Android虚拟设备上运行的TCP服务器. TCP客户端在计算机A上运行,而android仿真器在计算机B上运行.

I ask this, because I am trying to create a TCP client which will connect via socket to a TCP server running on an Android Virtual Device. The TCP client runs on machine A and the android emulator runs on machine B.

我已经查看了Android模拟器的端口转发,并且了解到该模拟器在虚拟路由器后面运行,并且我必须配置端口转发,以便可以将TCP套接字连接从一个端口路由到另一个端口,但是仅这些文档有关于如何从回送接口(所谓的本地主机)进行端口转发的说明,因此很明显,如果我尝试为非回送接口的接口创建套接字,则无法获得TCP套接字转发到仿真器,我的TCP连接失败.

I have looked up port forwarding for Android Emulators, and have learned that the emulator runs behind a virtual router, and that I must configure port forwarding such that TCP socket connections can be routed from one port to another, but these documents only have instructions on how on how to port forward from the loop back interface (the so called local host), so obviously, if I try to create a socket to an interface which is not the loop back interface, I don't get my TCP socket forwarded to the emulator, and my TCP connection fails.

所以我想知道回送接口上的端口5000在面向外的网络接口上是否有表兄端口5000?

So I wonder if port 5000 on the loop back interface has cousin port 5000 on an outwards facing networking interface?

我想知道是否可以在较低的级别上将端口从面向外的接口端口5000转发到环回接口端口5000,然后环回接口端口5000将转发给位于其上的仿真器端口.

And I wonder if at some low level I can port forward from the outward facing interface port 5000 to the loopback interface port 5000 which will then forward to port the emulator is situated on.

这可能吗?

推荐答案

adb -s emulator-5554 forward tcp:5000 tcp:6000
echo "1" > /proc/sys/net/ipv4/ip_forward
sudo sysctl -w net.ipv4.conf.enp0s31f6.route_localnet=1
sudo iptables -t nat -A PREROUTING -p tcp --dport 5001 -j DNAT --to 127.0.0.1:5000
sudo iptables -A FORWARD -p tcp -d 127.0.0.1 --dport 5000 -j ACCEPT

这篇关于我可以从一个接口端口转发到另一个接口端口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆