Java - getting server hostname and / or ip address from client

So here is my situation. I need to use sockets when creating connections between server and client. It is impossible to agree. I have a server that is running and listening using something like this

ServerSocket serverSocket = new ServerSocket(portNumber); while (listening) { new MultiClientThread(serverSocket.accept()).start(); } 

and I need a client to connect to the "portNumber" that is listening. The problem is that I use this line of code for the client.

 Socket socket = new Socket(hostName, portNumber); 

And I do not know how to get the "hostName" part for the parameters. Is it possible to get "hostName" if I knew the port number that they were listening to? Or maybe another way of saying this is how I can connect to a server listening on a port using tcp connections.

+6
source share
1 answer

hostName usually hardcoded in the client. This can be an IP address or a domain name. If the server is running on the same computer, you can use localhost or 127.0.0.1 as the host name.

+4
source

Source: https://habr.com/ru/post/958204/


All Articles