Fastest way to test internet connection

前端 未结 6 1606
别那么骄傲
别那么骄傲 2020-11-30 04:03

C# 2008 SP1

I am using this code to connect to our client website. This is for a softphone application. Before the user makes a call, the softphone has to test if th

相关标签:
6条回答
  • 2020-11-30 04:35

    I had a similar situation where we were going to make a WCF call to a client and it would take too long if the client wasn't reachable (for whatever reason). What I did was open a raw TCP socket to the client address... This fails quickly if the client is not listening at the port (in your case port 80) and succeeds quickly if they are there.

    This gave me the fastest and most accurate answer to determine if I would be able to communicate with the endpoint that I was trying to reach. The only down side of this is that you have to manage the timeout yourself on the socket because there are only options for Send and Receive timeouts not connect.

    It goes something like this...

    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
        try
        {
             IAsyncResult result = socket.BeginConnect("www.xxxxxxxxx.com", 80, null, null );
             //I set it for 3 sec timeout, but if you are on an internal LAN you can probably 
             //drop that down a little because this should be instant if it is going to work
             bool success = result.AsyncWaitHandle.WaitOne( 3000, true );
    
             if ( !success )
             {
                    throw new ApplicationException("Failed to connect server.");
             }
    
             // Success
             //... 
        }
        finally
        {
             //You should always close the socket!
             socket.Close();
        }
    

    I don't have the actual code that I used in front of me, but this should put you on the general path.

    0 讨论(0)
  • 2020-11-30 04:36

    You can reference the Microsoft.VisualBasic.Devices namespace to use the NetworkAvailableEventHandler delegate and Network class. I use Network.IsAvailable property and Network.NetworkAvailabilityChanged event to check that the network is there (or is affected later), and then do an HTTP GET to my server to see if the server is there.

    This helps with reporting the issue a bit more specifically, instead of "can't see network".

    0 讨论(0)
  • 2020-11-30 04:37

    What is the softphone going to use for its real communication? Is that going over HTTP/HTTPS to the same web site? If so, that's absolutely the right way to go - the next step is to work out why it's taking so long.

    Is the network connection definitely up before you may the request? Are you definitely not making any requests beforehand? I ask because I notice you're not disposing of the response - if that happens elsewhere as well, you may find that you're running up against the connection pool only giving you a couple of connections to a particular server. The moral is to always put HttpWebResponses in a using statement. Of course, that may well not be the problem in your case, but it's worth a try.

    If your actual application is going to connect elsewhere, then that's where your test should check as well. Basically, make it as close to the real thing as possible.

    Out of interest, you say it's a "softphone" application - is this actually running on a phone of some description, using the compact framework, or is it a desktop app?

    0 讨论(0)
  • 2020-11-30 04:52

    A better option is to add reference to the Microsoft.VisualBasic and add a using statement Microsoft.VisualBasic.Device then you can use the following line of code to check for connection to any network at all.

    public static bool isConnectedToNetwork {
       get {
          Network network = new Network();
          return network.IsAvailable;
       }
    }
    
    0 讨论(0)
  • 2020-11-30 04:56

    request.GetResponse() itself will give you an exception if the host name can't be resolved, though, so you might want to put a try-catch around that, catch System.Net.WebException, and check its error status in the Status property. I'm not sure which statuses exactly would indicate no internet, though; most responses could just as well be due to dns or other connection problems.

    0 讨论(0)
  • 2020-11-30 04:58

    Try using P/Invoke to call InternetGetConnectedState. That should tell you whether or not you have a connection configured. You can then try checking the specific connection to your service using InternetCheckConnection. This is (sometimes) quicker than hooking up the connection directly, but I'd test it to see if it's any better than just doing a full connection up front.

    0 讨论(0)
提交回复
热议问题