Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Losing network connection

Posted on 2001-07-12
8
Medium Priority
?
317 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

824 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