问题
I have some software running on port 50885 on a computer behind a NAT
How can I access this port from the internet? I can initiate something from the computer as I have to launch the app.
If I get it correctly : with my local address:port, lets say : 192.168.0.10:50885 If something goes trough the NAT initiated by the computer, Then there should be some sort of port forwarding created automatically and there should be some sort of publicly IP:PORT that should knock on the 50885 port on the computer behind the NAT. Am I right?
If so, wouldn't it be possible to ask a server-side script like in php, Hey, tell me what this publicly IP & Port are?
I'm a bit confused about Port Forwarding.
回答1:
Per my understanding, assuming you use socket local_ip:local_port to connect remote1_ip:remote1_port. After NAPT device local_ip:local_port becomes translated_ip:translated_port. remote1 can get translated_ip:translated_port but I have not found such service. Next time you establish a new connection with remote2_ip:remote2_port with the same client local_ip:local_port, will translated_ip:translated_port be kept unchanged? Normally NAPT sever will reserve it for some time, say 5 minutes. So if someone from remote3:remote3_port to connect translated_ip:translated_port within 5 minutes (since the last packet sent to translated_ip:translated_port), NAPT server will direct traffic to local_ip:local_port. If remote3_port=remote1_port, though remote1_ip!=remote1_ip, the connection will be accepted by the local socket. If the information expired, NAPT sever will allocate a new translated_ip2:translated_port2.
回答2:
Yes, you would have to query an external script to retrieve the public info from the other side of the router. There are plenty of websites to get the IP (http://whatismyip.com, http://iplookup.flashfxp.com, etc) but I have never seen one that reports both the IP and Port, they usually only report the IP. Most NATs use a 1-to-1 relationship on ports, though they CAN use different ports. If you have your own website (or have a friend that does), it would not be very difficult to write your own script to retrieve the public port.
If you make an OUTBOUND TCP connection from your LAN PC through the router to the outside, then the router generates and keeps track of the necessary NAT lookup info automatically so it can then forward traffic back and forth for that connection.
If you make an INBOUND TCP connection from the outside through the router to your LAN PC, there is no NAT lookup info established automatically. The router needs to be told ahead of time, via a Port Forwarding rule, which private LAN IP:Port to direct an incoming connection to when it receives a new client connection on a specific public IP:Port. If your router supports uPNP (and if it is enabled) then your app can use a uPNP API/library to programmably create a Port Forwarding rule in the router when the app opens a listening socket. Otherwise, if uPNP is not available then you have to configure the router manually instead.
来源:https://stackoverflow.com/questions/21768007/get-the-nat-translated-ip-and-port-of-some-local-port