Tuesday, May 3, 2011

Belajar Visual Basic: Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname

Belajar Visual Basic: Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname



Private Sub Form_Load()

Timer1.Enabled = True

LblCekMyIP1 = "IP Host Name: " & GetIPHostName

LblCekMyIP2 = "IP Address: " & GetIPAddress()

End Sub

Private Sub Timer1_Timer()

If InternetGetConnectedState(0&, 0&) = 1 Then

SB.Panels(1).Text = "Status: Terhubung dengan Internet"

Else

SB.Panels(1).Text = "Status: Tidak terhubung dengan Internet"

End If

End Sub

=============================

Letakkan code di bawah Ini pada Modul

=============================

'cek koneksi internet

Public Declare Function Internet

GetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

'---------CEK IP Adress komputer dan HOST NAME-----

Public Const MAX_WSADescription = 256Public Const MAX_WSASYSStatus = 128'

Public Const ERROR_SUCCESS As Long = 0

Public Const WS_VERSION_REQD As Long = &H101

Public Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&

Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&

Public Const MIN_SOCKETS_REQD As Long = 1Public Const SOCKET_ERROR As Long = -1

Public Type HostenthName As LonghAliases As LonghAddrType As IntegerhLen As IntegerhAddrList As Long

End Type

Public Type WSADATAwversion As IntegerwHighVersion As IntegerszDescription(0 To MAX_WSADescription) As ByteszSystemStatus(0 To MAX_WSASYSStatus) As BytewMaxSockets As IntegerwMaxUDPDG As IntegerdwVendorInfo As Long

End Type

Public Declare Function WSAGetlastError Lib "wsock32.dll" () As Long

Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired As Long, lpWSAdata As WSADATA) As Long

Public Declare Function WSACleanup Lib "wsock32.dll" () As Long

Public Declare Function gethostname Lib "wsock32.dll" (ByVal szHost As String, ByVal dwHostLen As Long) As Long

Public Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal szHost As String) As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvdest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Public Function GetIPAddress() As String

Dim sHostName As String * 256

Dim lpHost As Long

Dim HOST As Hostent

Dim dwIPAddr As Long

Dim tmpIPAddr() As Byte

Dim i As Integer

Dim sIPAddr As String

If Not SocketsInitialize() Then

GetIPAddress = ""

Exit Function

End If

If gethostname(sHostName, 256) = SOCKET_ERROR Then

GetIPAddress = ""MsgBox "Windows Sockets Error " & Str$(WSAGetlastError()) & " has occurred. Host Name tidak dapat ditampilkan."SocketsCleanup

Exit Function

End If

sHostName = Trim$(sHostName)

lpHost = GetHostByName(sHostName)

If lpHost = 0 Then

GetIPAddress = "" MsgBox "Socket Windows tidak memberikan respon. " & "Host Name tidak dapat ditampilkan." SocketsCleanup

Exit Function

End If

CopyMemory HOST, lpHost, Len(HOST)CopyMemory dwIPAddr, HOST.hAddrList, 4ReDim tmpIPAddr(1 To HOST.hLen)CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLenFor i = 1 To HOST.hLensIPAddr = sIPAddr & tmpIPAddr(i) & "."NextGetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)SocketsCleanup

End Function

Public Function GetIPHostName() As StringDim sHostName As String * 256If Not SocketsInitialize() ThenGetIPHostName = ""

Exit Function

End If

If gethostname(sHostName, 256) = SOCKET_ERROR ThenGetIPHostName = ""MsgBox "Windows Sockets Error " & Str$(WSAGetlastError()) & " has occurred. Host Name tidak dapat ditampilkan."SocketsCleanup

Exit Function

End IfGetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)SocketsCleanup

End Function

Public Function HiByte(ByVal wParam As Integer)HiByte = wParam \ &H100 And &HFF&

End Function

Public Function LoByte(ByVal wParam As Integer)LoByte = wParam And &HFF&

End Function

Public Sub SocketsCleanup() If WSACleanup() <> error_success_ Then MsgBox " Socket Error terjadi dalam CleanUp."

End If

End Sub

Public Function SocketsInitialize() As Boolean

Dim WSAD As WSADATA

Dim sLoByte As String

Dim sHiByte As StringIf WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS ThenMsgBox "Socket Windows 32-bit tidak respon"SocketsInitialize = False

Exit Function

End If

If WSAD.wMaxSockets = MIN_SOCKETS_REQD Then MsgBox "Aplikasi ini membutuhkan minimum " & CStr(MIN_SOCKETS_REQD) & " Socket yang support." SocketsInitialize = False

Exit Function

End If

If LoByte(WSAD.wversion) < shibyte =" CStr(HiByte(WSAD.wversion))" slobyte =" CStr(LoByte(WSAD.wversion))MsgBox" socketsinitialize =" False">
Exit Function
End If SocketsInitialize = True
End Function

Description: Belajar Visual Basic: Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname Rating: 4.5 Reviewer: ukungzulfah - ItemReviewed: Belajar Visual Basic: Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname Posted by: Get Answer
Get Answer, Updated at: 8:42 AM