Thủ thuật: Kiểm tra cổng mở hay chưa
Tác giả: dacting93Mô tả: Kiểm tra cổng mở hay chưa
Thực ra cách này em dựa vào một trang web để kiểm tra thôi.
Do học lập trình không đến nơi đến chốn + Đam mê đã ra 1 đoạn mã nguồn rất củ chuối. Anh em tham khảo, rồi dạy em cách tách chuỗi nha
Code from 1.
Gồm:
1 button
2 label
1 textbox
Dim src As String
Function Tach(chuoi As String)
Dim a, b As Integer
Dim kq As String
kq = chuoi
' Tim dau hieu cua su open Port
kq = Replace(kq, "Port " & Text1.Text & " is open and accepting connections.", "`", 1, -1, vbBinaryCompare)
kq = Replace(kq, "Port " & Text1.Text & " is open and accepting connections.", "`", 1, -1, vbBinaryCompare)
'Tach bang For
' Lan tach thu nhat
For a = 1 To 95
kq = Replace(kq, Chr(a), "", 1, -1, vbBinaryCompare)
Next a
' Lan tach thu hai
For a = 97 To 128
kq = Replace(kq, Chr(a), "", 1, -1, vbBinaryCompare)
Next a
kq = Replace(kq, "`", "Port " & Text1.Text & " is open and accepting connections.", 1, -1, vbBinaryCompare)
kq = Right(kq, Len(kq) - 34)
b = Len("Port " & Text1.Text & " is open and accepting connections.")
kq = Left(kq, b)
Tach = kq
End Function
Private Sub Command1_Click()
Label2.Caption = "Preparing for checking"
src = GetUrlSource("http://www.utorrent.com/testport?port=" & Text1.Text)
Label2.Caption = Tach(src)
If Label2.Caption <> "Port " & Text1.Text & " is open and accepting connections." Then
Label2.Caption = "Port " & Text1.Text & " is not open and accepting connections."
End If
End Sub
Code module
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Const IF_FROM_CACHE = &H1000000
Public Const IF_MAKE_PERSISTENT = &H2000000
Public Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256
Public Function GetUrlSource(sURL As String) As String
Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
Dim hInternet As Long, hSession As Long, lReturn As Long
hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
If hInternet Then
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sBuffer
Do While lReturn <> 0
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sData + Mid(sBuffer, 1, lReturn)
Loop
End If
iResult = InternetCloseHandle(hInternet)
GetUrlSource = sData
End Function
Cứ nhập cổng cần check rồi bấm vô check.
Cái này có thể áp dụng để check nhiểu cổng một lúc. Rất hữu dụng cho những người nào hay download = torrent để kiểm tra cổng. Đôi khi kiểm tra cổng nào đang "bị" mở để đóng vào tránh bị đột nhập
Ý tưởng thì hay nhưng em code kém quá, ai sửa hộ em với