Solved

Losing network connection

Posted on 2001-07-12
8
306 Views
Last Modified: 2010-05-18
I have a vb application that I need to determine a way to verify a network connection still exist.  I need to do this because if the network goes down, the application still needs to be able to run.

The only way I found to do this was to use WSOCK32.dll.  In the app, I have created a timer.  Everytime this timer triggers, I check the connection using WSOCK32.DLL.  This works fine when there is a connection.  The problem is when the network cable is unplugged (or there is not a network connection) this process takes at least two minutes to check and it still doesn't return the value I expect.  The code I am using is as follows:

In a module:
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long

'****************************
'*  Constant Declarations        *
'****************************
Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128

'****************************
'*  Type Declarations        *
'****************************
Private Type HOSTENT
   hName As Long
   hAliases As Long
   hAddrType As Integer
   hLength As Integer
   hAddrList As Long
End Type

Private Type WSADATA
   wversion As Integer
   sHighVersion As Integer
   szDescription(0 To WSADescription_Len) As Byte
   szSystemStatus(0 To WSASYS_Status_Len) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   ipszVendorInfo As Long
End Type

'*** Call this routine at startup
Public Sub InitializeWinsockAPI()
    On Error Resume Next
    Dim lStartWinsock As Long
    Dim WSad As WSADATA
    lStartWinsock = WSAStartup(WS_VERSION_REQD, WSad)
End Sub

'***Call this routine on exit
Public Sub CleanupWinsockAPI()
    On Error Resume Next
    Dim lWinSockCleanup As Long
    lWinSockCleanup = WSACleanup()
End Sub

Public Function GetHostIP(sPCName As String) As String
   Dim hostent_addr As Long
   Dim lStartWinsock As Long
   Dim WSad As WSADATA
   Dim lWinSockCleanup As Long
   
   lStartWinsock = WSAStartup(WS_VERSION_REQD, WSad)
   
   hostent_addr = gethostbyname(Trim$(sPCName))
   
   If hostent_addr = 0 Then
      GetHostIP = False
   Else
      GetHostIP = True
   End If
   
   lWinSockCleanup = WSACleanup()
End Function


In my form:

If GetHostIP(gNetwork) Then
   AS400.Active = True
Else
   AS400.Active = False
   Exit Sub
End If

If I am running the application and unplug the network cable, I expect the value of the following code to be 0 and it isn't:
If hostent_addr = 0 Then
      GetHostIP = False
   Else
      GetHostIP = True
   End If

If I start the application when I am not connected to the network, the value of hostent_addr is 0.  I am not sure why this is.  

My biggest issue, though, is the length of time it takes to perform this check when I don't have a network connection.  I would like to know if anyone has a suggestion of a better way to check for a network connection or if there is any way I could speed up my current process.  

Thanks in advance for the help.
   
 
0
Comment
Question by:DebeG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Expert Comment

by:Otana
ID: 6277782
there's an easy ping function that uses a few API calls. Unfortunately, I don't have it here. If you don't have an answer in say, three hours, I'll try to post it.
0
 
LVL 20

Accepted Solution

by:
hes earned 100 total points
ID: 6277879
Declare this

Private Declare Function IsNetworkAlive Lib "Sensapi" _
  (lpdwFlags As Long) As Long

To check the connection

Dim tmp As Long
Dim Status As Boolean
   
Status = IsNetworkAlive(tmp)
Returns True if Network Connection is active
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278011
Sensapi?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278018
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278021
0
 
LVL 20

Expert Comment

by:hes
ID: 6278035
Yep Sensapi.dll
0
 

Author Comment

by:DebeG
ID: 6280588
Thank you.  the "Sensapi" is what I was looking for, although I did not use IsNetworkAlive.  I wanted to check a specific IP address so here is what I used:

Public Declare Function IsDestinationReachable Lib "Sensapi.dll" _
   Alias "IsDestinationReachableA" _
  (ByVal lpszDestination As String, _
   lpQOCInfo As QOCINFO) As Long

The other posting gave an example of basically what my code stated I was doing by using the wsock.dll.  That was not acceptable because it took too long when there was not a network connection.  

The sensapi.dll works much faster.  Thanks again!
0
 
LVL 20

Expert Comment

by:hes
ID: 6280664
Glad to have helped
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question