CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Trying to establish if Internet connection is available from a service

    I work on the code that has a worker thread in a local service application that needs to send a lengthy submission to a web server via an HTTP connection. Such submission is scheduled once so often (say, once every 24 hours.) So the current algorithm simply tries to do the transmission to the web server and then re-schedules for the next time. The problem is that at times the Internet connection might not be available on a client computer. So my idea was to check if Internet is available and if it is to continue on with the submission and if it is not, then to reschedule in a little while (in 30 seconds, for instance) to retry again.

    So I did some search and found the InternetGetConnectedState() API that should do just that. Very simple, right? Well, wrong ... I kept reading the MSDN documentation and found this little sentence:
    Quote Originally Posted by MSDN
    In addition, it should not be used from a service.
    I was able to find out that this warning is there due to the fact that evidently that API can display a user diaglog (for no apparent, or at least documented, reason.)

    So my question is, since I can't use that API, what shall I use instead?


    PS. To prevent answers, such as, "continue on submitting your data and handle errors" -- First off, there's "a million" socket errors that may result from inability to connect vs. server's inability to respond. So I need to clearly know, is it the client computer that simply doesn't have an Internet connection at the moment, or if it's a server issue. And in the latter case I will not make this service push on another submission in 30 seconds and swamp the server even more.

    Secondly, the process of submission takes a lot of client computer resources to generate and submit, so it'd be very nice to know right before I start this "costly" process, whether the most basic prerequisite is actually available -- i.e. Internet connection -- before I begin it.

    So hopefully someone knows a better way of finding out if an Internet connection is available from a local service...

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Trying to establish if Internet connection is available from a service

    You have the basic problem that even if your 'IsInternetConnected' code can test that the web server is available, there is no guarantee that it will be available when you really try it.

    Btw, I've read the comments regarding not using the api in a service and the msdn suggestion was to use WinHttp as a replacement. Unfortunately, I didn't come up with anything that shows exactly how to use WinHttp to check an internet connection.

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Trying to establish if Internet connection is available from a service

    Yes, thanks. I understand that it creates some sort of a "race condition" but it is not a problem in this case, if I get a "false-positive" result from such API. What are the odds of that happening? 1 in a 1000. And even if it happens then, the only penalty I'll get is a wasted preparation for the web server submission, which will be happening anyway if I don't use such Internet connection check API.

    As WinHttp go, I could not find a single API to do what I want either. Those WinHttp APIs seem to be a lower level socket APIs to establish the connection. Do they mean that I have to write my own checker with that comment?

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Trying to establish if Internet connection is available from a service

    Well, I'm no expert with services, therefore not sure if following may work for you or not, you'll have to see if it works for you or not.

    INetworkConnection::get_IsConnectedToInternet

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Trying to establish if Internet connection is available from a service

    According to this arcticle:
    Using Win32_NetworkAdapterConfiguration find the network device that has the lowest IPConnectionMetric, this will be the first device used for internet access.
    Best regards,
    Igor

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Trying to establish if Internet connection is available from a service

    Thank you, fellas. Much appreciated.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Trying to establish if Internet connection is available from a service

    it's not just InternetGetConnectedState() that doesn't work from a service. it's the entire suite of WinINET functions that don't work reliably from a service.

    This leaves either WinHttp or sockets to do any internet stuff from a service. And this poses some extra problems.

    When you use WinINET, you are effectively using the IE settings for accessing the internet. Security, firewall, proxy, etc.

    If you use WinHttp and/or sockets, you will need to do some extra work making your own internet settings dialog and allowing for all the things that make accessing the internet nasty bussiness (firewalls/proxy servers etc).


    If you are using winhttp and/or sockets from a service however... don't bother with trying to check if a connection is up. There's no such thing as the "dialer" in a service, you ether have access or you don't. just make a connection to the server. If it fails, you have no internet, or the server isn't running, or one of the other internet issues...


    the whole reason for the existance of InternetGetConnectedState() is to check if the internet is available WITHOUT triggering the dialer (which would happen with a straightforward connect call if set to autodial).
    THe good news of course... use of the dialer is dropping fast, permanent internet via cable/dsl is fast becoming the norm.
    Last edited by OReubens; July 7th, 2012 at 11:58 AM.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Trying to establish if Internet connection is available from a service

    Quote Originally Posted by OReubens View Post
    the whole reason for the existance of InternetGetConnectedState() is to check if the internet is available WITHOUT triggering the dialer (which would happen with a straightforward connect call if set to autodial).
    Yes, I was exactly under the same assumption. I believe I started using InternetGetConnectedState() back in the day (while programming user-mode code) to prevent the dial-up dialogs from popping up.

    So, if that's the case, why does MSDN have that warning? Or is it just a sloppy "copy and paste" job from one of their interns unlucky enough to get a job of editing MSDN pages?

    Quote Originally Posted by OReubens View Post
    THe good news of course... use of the dialer is dropping fast, permanent internet via cable/dsl is fast becoming the norm.
    I was trying to emulate this "dialer" to pop up to see how my code will run on some system with an antiquated dial-up connection but I couldn't. (Well, I'm obviously connected to a wireless high speed Internet.) Any idea how to bring up that "dialer"?

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Trying to establish if Internet connection is available from a service

    The "wininet doesn't work for server implementations and from a service" is present on all the WinInet pages.
    The warning is valid. you shouldn't call this function from a service.

    I'm not sure that in an old dialup setup you could even have a service dial out, afaik, it could only use an existing connection which may have needed manual dialing. So all a service could do was periodically check if a connection to the server could be made.

    I'm not sure how you can get the dialer to still pop up (if that is even still present in new versions of windows), you'll probbaly have to reinstall/configure how your pc connects to the internet for starters.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured