S
S
Stavr Strelov2020-08-05 16:50:17
Java
Stavr Strelov, 2020-08-05 16:50:17

Java. How to connect client to server via Socket?

I'm developing a client-server game, but recently I ran into a strange problem.
I am connecting to a server running on my computer as a separate program via Socket: However, when creating it, you must specify the host and port. What is Port is clear. I ask it myself. But there are problems with Host. I know that in its place you need to enter the external ip-address of my computer, which can be found on the site 2ip.ru. I enter it, and everything works on my computer, but when I turn on the program on another computer, the other computer cannot connect to my server. I have no idea what could be the problem. Help!!
Socket socket = new Socket(host, port);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Timur Pokrovsky, 2020-08-05
@Makaroshka007

Try like this:
Socket socket = new Socket("localhost", port);

A
Alexey Cheremisin, 2020-08-05
@leahch

1) your server must be accessible from the Internet, have a routed ip-address.
2) on your computer, you need to allow the server port on the firewall.
In other words, if you want to connect to your server from the outside, then place it somewhere on a virtual machine from a virtual machine provider with a dedicated IP address.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question