Solved

Losing network connection

Posted on 2001-07-12
8
304 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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 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