Solved

Losing network connection

Posted on 2001-07-12
8
305 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 61
VBA filters 2 65
RUNRMTCMD from AS/400 12 69
Problem to line 23 54
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

839 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