-1

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.

Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51

2 Answers2

1

Did you check if you have a proxy internet to configure (perhaps with a login and password) ?

If you are on Windows, try to execute "cmd" (the Windows command line) and type : tracert varzesh3.com Check also your network configuration about proxy.

  • I tracert the varzesh3.com and it finishes successfully . Of course I have test my socket to the google too. but again when I run it with IDE it is ok but when I run builded jar file It fails to connect – Behzad Hassani Mar 13 '15 at 20:14
  • Thanks to try for the tracert. With the result of your tracert, at the first line, you should have an IP address. Could you try to test this particular IP with a new builded jar please ? – Florian Prud'homme Mar 13 '15 at 20:30
  • Yes I tested it, But nothing changed :( – Behzad Hassani Mar 13 '15 at 20:33
  • Sorry I really do not know what happened with your jar . I've tried myself to build a jar like you and it works fine . I really suspect an http proxy could be culprit. You can try this alternative too : http://stackoverflow.com/a/26583714/4660239 – Florian Prud'homme Mar 13 '15 at 20:41
  • Thank you buddy . I founded it :) I will share my answer with you. Thank you for your help :) :) ♥ – Behzad Hassani Mar 13 '15 at 20:43
0

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);
        }
    }
Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51