简体   繁体   中英

How to specify full ip address to the local server?

"Full ip" means global ip(I have a static) + local ip( 192.168.0.xxx - address in my local network). I wrote simple Server and Client application using java.net.ServerSocket and java.net.Socket relatively. On the localhost it working good. But I'm interesting in running server app on the computer in my local network(router) and connecting to it from everywhere using Client app. Connection is established as follows:

Socket socket = new Socket(IP, PORT);

I tried my absolute static ip(but it's router ip), router ip + local ip( 192.168.0.100 ) it's all not working. How can I specify the full path to my "server" in router local network?

I think you misunderstood IP addresses.

Any IP address is just a SINGLE IP address. There is no such thing as a "relative" IP address, like you have a relative path in a file system.

Suppose you have an externally-visible router IP of, say 136.23.75.30. You then have a local subnet of 192.168.xy, where your own host has an address of 192.168.1.100 and your router has address of 192.168.1.1 .

In that situation, when you connect to your host from inside your subnet, you can specify the host's address on the subnet, 192.168.1.100 .

However, when you connect from the Internet at-large, the only IP you can connect to is you router's external IP, as provided by your ISP. In our example it's 136.23.75.30 . Any attempt to connect to 192.168.1.100 from any location outside of your own subnet will go to a wrong host or nowhere at all.

How can you accomplish what you need? The secret is in your router's configuration. You need to set the router up for port forwarding. Basically, your router will take the incoming connection on certain port (let's say 8888) and turn that connection into the connection to your host, 192, 168.1.100 on, for example, the same port, 8888.

In other words, the outside world thinks that it connects to 136.23.75.30, while in reality the router makes sure that 192.168.1.100 is responding.

Assuming this is a home setup, you need to get to you router's admin screen. You can do it by putting " http://192.168.1.1 " into your browser's address bar. After providing suitable authentication (usually printed on the router's back) you need to find some sort of advanced setup tab, and look for "Port Forwarding". There you usually enter the target address, 192.168.1.100, and the source and target port (let's say both are 8888).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM