James
James

Reputation: 377

Java socket client to server on different network.

My question: How do I establish a connection from a client on one network to a server on another network in Java?

Background: I created a simple client and server that communicate fine using localhost, or on two different computers using the same network. I connect using the inet address (e.g. inet 10.xx.xx.xx) and a port number (e.g. 55123).

I realize that my client cannot find my server because they are on different networks, i.e. the ip address and port do not provide enough info for the client to find the server.

sock = new Socket(10.xx.xx.xx, 55123);

What am I missing for my client on the other network to find my server?

Thank you very much for your help!

James

Upvotes: 1

Views: 3174

Answers (2)

Stephen C
Stephen C

Reputation: 718826

Your problem is not Java specific. This is really about how IPv4 / IPv6 networking / addressing works.

The 10.0.0.0/24 network range is reserved for private networks; see Wikipedia's "Private Network" article.

If the server has a 10.xx.xx.xx address, AND your client is on a different network, then it is simply not possible for your client to connect directly to the server. Your client's network simply cannot route packets to the server's network.

(Indeed, there are millions of networks around the world that use 10.xx.xx.xx addresses. Your server's IP address is not unique ... except in the context of the private network it is attached to. So which of the many possible servers with IP of (say) 10.42.42.42 servers should the client's packets be routed to?)

Possible solutions:

  • Give the server needs a public IP address.
  • Set up 3rd server with a public IP address that is also on the private network, and run some kind of service to tunnel traffic to the server. A VPN is one way of doing this.

Upvotes: 2

alaster
alaster

Reputation: 4171

Server must have public IP so that client can connect to it. Or you need VPN over those networks (so server is reachable from client's network).

Upvotes: 1

Related Questions