Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname

Cek Koneksi Internat (On/Off), Cek IP Adress, Cek Hostname 

Sekarang kita mencoba membuat aplikasi yang berfungsi untuk mengetahui Status Kmputer terhubung dengan Internet atau tidak, Mengetahui IP Adress saat tidak terhubung dengan internet dan IP Adress saat terhubung dengan Internet Serta mengetahui IP Host Name. Untuk Lebih jelasnya dapat anda perhatikan kedua gambar di bawah ini yaitu Gambar aplikasi saat komputer tidak terhubung dengan internet (IP Adress otomatis 127.0.0.1) dan Gambar aplikasi saat terhubung dengan internet maka IP Adress komputer berubah menjadi 10.242.39.122 dan pada waktu yang lain ternyata IP Adress komputer berubah kembali menjadi
Berikut ini adalah Source codenya. Langsung saja yang dibutuhkan dalam pembuatan aplikasi ini adalah :
- 2 label dengan property name LblCekMyIP1 dan LblCekMyIP2
- 1 timer dengan property name Timer1, iNTERVAL = 1000
- Winsock1, untuk menambahkan Winsock1 pada toolbox maka dengan cara klik kanan pada toolbox pilih component dan centang microsoft winsock control 6.0
- status bar dengan property name SB, untuk menambahkan status bar pada toolbox caranya sama dengan winsock tetapi pilih windows common controls 6.0(sp6) Kemudian setelah status bar ditambahkan dalam form maka klik kanan status bar tersebut pilih property dan pada tab panel pilih angka 2 pada textbox Autosize.
- 1 modul untuk source code cek koneksi internet dan ip adress/Host name- 1 form
Semoga bermanfaat, terimakasih.
========================================
‘COPY PASTEKAN KODE DI BAWAH INI PADA FORM
========================================
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
Semoga bermanfaat.
Previous
Next Post »
Thanks for your comment