0
package com.company;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.*;

public class Main {
public static void main ( String [] args ) throws UnknownHostException
{

    //Finding Ip
    InetAddress address = InetAddress.getLocalHost();
    String hostIP = address.getHostAddress() ;
    String hostName = address.getHostName();


    //Finding Free Port
    int startPortRange=0;
    int stopPortRange=65365;
    for(int i=startPortRange; i <=stopPortRange; i++)
    {
        try
        {
            Socket ServerSok = new Socket(hostIP,i);
            System.out.println("Port in use: " + i );
            ServerSok.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.out.println("Port not in use: " + i );
}

}

When I run the code it says that variable "i" is not found. How should I fix this? I had this issue with the ip Finding section and someone helped me fix it with putting throw next to static void but I don't know how to do it with this one.

  • 2
    No, it's that `i` can't be found outside the for loop. The `try/catch` isn't relevant. – Andy Turner Oct 01 '21 at 13:36
  • What are you even trying to say there? It looks like you'd want to say "All ports in the range start..stop are in use". Whatever value `i` has after the loop doesn't seem especially relevant. – Andy Turner Oct 01 '21 at 13:38

0 Answers0