1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
Imports System.Net.Sockets
Imports System.Net
Public Class Pinger
Private _socket As Socket
Private _Connected As Boolean
Private _sw As Stopwatch
Public Function ping(ByVal adapter As IPAddress, ByVal timeout As Integer) As Boolean
_Connected = False
_socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
_socket.Bind(New IPEndPoint(adapter, 0))
_socket.BeginConnect("google.fr", 80, AddressOf ConnectedCallBack, _socket)
_sw = New Stopwatch()
_sw.Start()
While _sw.ElapsedMilliseconds() < timeout And _sw.IsRunning
System.Threading.Thread.Sleep(1)
End While
Return _Connected
End Function
Public Sub ConnectedCallBack(ByVal ar As IAsyncResult)
Try
Dim s As Socket = CType(ar.AsyncState, Socket)
s.EndConnect(ar)
_Connected = True
s.Close()
Catch
_Connected = False
End Try
_sw.Stop()
End Sub
End Class |