How to connect a socket over the internet

Asked

Viewed 334 times

-2

I recently started studying sockets and I know that to create a server I use a port and to connect a client I use the port and the server ip on the local network, but I would like to connect a client to a server through the internet (obviously in different networks) the question is, which ip should I put?

    ServerSocket server = new ServerSocket("xxx.xxx.xxx", 1234);

1 answer

1


Hello,

There are basically three possible connection cases, see:


1: In offline testing, simulating a server on your own machine, you use IP 127.0.0.1, the so famous localhost. This IP refers to the actual running computer of that particular code.

2: In the case of a server within the same network, you can use the local IP of the computer. This IP is assigned by the router when the machine connects to the network, and its pattern varies according to each router. The most common local IP standard is: 192.168.1.x, where x is the number corresponding to the computer.

3: If you wish, from one machine access another anywhere in the internet, you will need to know the public IP (ie IP visible to anyone on the internet) of your target server.


What you’re trying to do fits the latter case, so the IP you should put in to instantiate the Socket is the server’s public IP.

I hope I helped, hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.