0

I tried to check whether a port is free in a remote machine by trying to create a ServerSocket with port and IP address of the machine. IP is accessible from my machine. but it produces an Exception "Cannot assign requested address: JVM_Bind". I have seen many questions posted for this exception but does not answer for other than localhost.

my code is

  try {
                sock = hostName == null ? new ServerSocket(port) : new ServerSocket(port, 0, InetAddress.getByName(hostName));
                return true;
            } catch (Exception var14) {
                flag= false;
            } finally {
                if (sock != null) {
                    try {
                        sock.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

hostname - IP adress, port - port number

Antony
  • 11
  • 4
  • Have you seen this [this](https://stackoverflow.com/questions/8965155/cannot-assign-requested-address-jvm-bind) post? – J_D Mar 01 '19 at 06:45
  • yes, my code is the same as the code mentioned. – Antony Mar 01 '19 at 06:55
  • 1
    You cannot open server sockets on remote machines. You can only open sockets on your own machine (although, if your machine has several IP addresses, you can open a server socket only on one of those IP address) – Thomas Kläger Mar 01 '19 at 07:11
  • how do I check if the port is free in remote machine ? or is it possible to check whether a port is free on a remote machine? – Antony Mar 01 '19 at 08:34
  • Why do you want to check if a port is free on a remote machine? – Thomas Kläger Mar 01 '19 at 09:28

0 Answers0