Behzad Hassani Behzad Hassani - 3 months ago 20
Java Question

Socket connection timeout Issue in built jar file

I'm new to socket programming, with lots of questions in mind.

I wrote a code to check internet availability. When running via netbeans IDE launch, It detects availability of internet connection...

enter image description here

Sentence in blue means internet connection available.

When running built jar, It throws a timeout error and doesn't check availability...

enter image description here

and the second step's image is :

enter image description here

Sentence in red means no internet access.

Here's code. Any idea on what's the problem? I'm confused because it's not a regular issue.

private void checkInternetConnection()
{
Socket socket = null;
try
{
socket = new Socket("varzesh3.com", 80);
socket.setSoTimeout(50000);
lblStatus.setForeground(Color.blue);
lblStatus.setText("اینترنت فعال است");
socket.close();
timer.stop();
}
catch (IOException ex)
{
lblStatus.setForeground(Color.red);
lblStatus.setText("اینترنت غیر فعال است");
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}


Thanks for your attention.

Answer

Thank to @Florian Prud'homme

Ok I change my internet check code with this code :) and it is working perfect :)

private void checkInternetConnection(){      
        try {
            Process proccess = java.lang.Runtime.getRuntime().exec("ping varzesh3.com");
            int result = proccess.waitFor();
            if(result == 0){
                lblStatus.setForeground(Color.blue);
                lblStatus.setText("اینترنت فعال است");
                timer.stop();
            }
            else{
                lblStatus.setForeground(Color.red);
                lblStatus.setText("اینترنت غیر فعال است"); 
            }

        } catch (IOException | InterruptedException ex) {
            Logger.getLogger(InternetChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
Comments