Results 1 to 13 of 13

Thread: [2005] Checking if online?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    [2005] Checking if online?

    Basically I need an Event for when your internet connection goes on, and off

    Or just a way to detect if its on or off, my game will use a chat & a mySQL database, and basically you can't play without internet connection

    So, does anyone know how? and if so give an example please and thankyou

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2005] Checking if online?

    you could use this API function

    Code:
    Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Integer, ByVal dwReserved As Integer) As Integer
    
    'FLAGS
    'Modem is busy.
    Public Const ModemConnectionIsBusy As Integer = &H8S
    
    'Internet connection is currently Offline
    Public Const InternetIsOffline As Integer = &H20S
    
    'Internet connection is currently configured
    Public Const InternetConnectionIsConfigured As Integer = &H40S
    
    'Internet connection VIA Modem.
    Public Const ModemConnection As Integer = &H1S

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] Checking if online?

    You could, but they only work for dialup and VPN connections. If you're connected through a router, like just about all DSL and cable connections, you need to ping an external host.

    However, since you're accessing a server over the 'net anyway, just assume that it's going to be online, and add some code to trap a connection failure. How you do this depends on how you are connecting in the first place.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Checking if online?

    How can I ping a site, like google, lol

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: [2005] Checking if online?

    my.Computer.Network.Ping

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Checking if online?

    vb Code:
    1. dim pingSender as New Ping()
    2. dim reply as pingReply = pingSender.Send("www.google.com")
    3. if reply.status = IPStatus.Success then
    4.   'success
    5. else
    6.   'failure
    7. end if

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] Checking if online?

    Quote Originally Posted by Icyculyr Fr0st
    How can I ping a site, like google, lol
    There's no need. Aren't you accessing a remote host anyway? You don't have to manually check whether the connection is still present, just carry on as if it were and your program flow should handle the case when it isn't.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Checking if online?

    Yes, but I wont be setting up the mySQL database for a while, therefore I need something that can check for a net connection
    ping, ping reply, and ip status, dont show up as classes to choose from (it gives errors saying they're not aswell)

    should I include something? (what should I?)

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] Checking if online?

    If you don't have anything to connect to then why do you need to check whether you can connect to it?

    I'm sorry but I can't see the problem.

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: [2005] Checking if online?

    Carefull with "dim reply as pingReply = pingSender.Send("www.google.com")"
    because if you don't have a network enabled it generates an exception so you must do a try catch in any of the solutions.
    Ping , pingreply is on the Net.NetworkInformation namespace

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Checking if online?

    I dont know how to import namespaces so they're being used in my script

    I also dont know how to use Try... Catch

    Can anyone give an example of both problems, thanks

    If you don't have anything to connect to then why do you need to check whether you can connect to it?

    I'm sorry but I can't see the problem.
    The problem is very very soon you will need connect to something, therefore, I am asking before I need it, so I know how

  12. #12
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Checking if online?

    for import namesapce use the imports instruction at the very beginning of your class

    vb Code:
    1. try
    2.   'your work here
    3. catch ex as exception
    4.   'something when wrong here
    5. end try

  13. #13
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Checking if online?

    I dont know how to import namespaces so they're being used in my script
    Just put this statement at the top of your form, the ping code will work then:
    vb Code:
    1. Imports System.Net.NetworkInformation

    As for not knowing how to use Try/Catch, that's where MSDN really comes in handy, loads more info than I could give. Just look it up. It's not difficult to understand the Try/Catch block.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

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