2011年9月1日 星期四

VB.net Get/Set Tcp

Public Const ERROR_ACCESS_DENIED = 5&
Private Const ERROR_BUFFER_OVERFLOW As Short = 111
Private Const ERROR_INVALID_PARAMETER As Short = 87
Private Const ERROR_NO_DATA As Short = 232
Private Const ERROR_NOT_SUPPORTED As Short = 50
Private Const ERROR_SUCCESS As Short = 0
Private Const MIB_TCP_STATE_CLOSED As Short = 1
Private Const MIB_TCP_STATE_LISTEN As Short = 2
Private Const MIB_TCP_STATE_SYN_SENT As Short = 3
Private Const MIB_TCP_STATE_SYN_RCVD As Short = 4
Private Const MIB_TCP_STATE_ESTAB As Short = 5
Private Const MIB_TCP_STATE_FIN_WAIT1 As Short = 6
Private Const MIB_TCP_STATE_FIN_WAIT2 As Short = 7
Private Const MIB_TCP_STATE_CLOSE_WAIT As Short = 8
Private Const MIB_TCP_STATE_CLOSING As Short = 9
Private Const MIB_TCP_STATE_LAST_ACK As Short = 10
Private Const MIB_TCP_STATE_TIME_WAIT As Short = 11
Private Const MIB_TCP_STATE_DELETE_TCB As Short = 12
Declare Function GetTcpTable Lib "Iphlpapi" (ByVal pTcpTable As IntPtr, ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer
Declare Function SetTcpEntry Lib "IPhlpAPI" (ByRef pTcpRow As MIB_TCPROW) As Integer
'==========================================================
Public Structure MIB_TCPROW
Public dwState As Integer
Public dwLocalAddr As Integer
Public dwLocalPort As Integer
Public dwRemoteAddr As Integer
Public dwRemotePort As Integer
End Structure
'===============Funcion================================
Private Function GetState(ByRef lngState As Integer) As String
Select Case lngState
Case MIB_TCP_STATE_CLOSED : Return "已經關閉"
Case MIB_TCP_STATE_LISTEN : Return "監聽"
Case MIB_TCP_STATE_SYN_SENT : Return "發送同步空閒字符"
Case MIB_TCP_STATE_SYN_RCVD : Return "接收同步空閒字符"
Case MIB_TCP_STATE_ESTAB : Return "未知"
Case MIB_TCP_STATE_FIN_WAIT1 : Return "結束等待1"
Case MIB_TCP_STATE_FIN_WAIT2 : Return "結束等待2"
Case MIB_TCP_STATE_CLOSE_WAIT : Return "關閉等待"
Case MIB_TCP_STATE_CLOSING : Return "關閉中"
Case MIB_TCP_STATE_LAST_ACK : Return "命令正確應答"
Case MIB_TCP_STATE_TIME_WAIT : Return "連接等待"
Case MIB_TCP_STATE_DELETE_TCB : Return "刪除TCP連接"
End Select
End Function
Private Function GetIpFromLong(ByRef lngIPAddress As Integer) As String
Dim arrIpParts() As Byte = BitConverter.GetBytes(lngIPAddress)
GetIpFromLong = CStr(arrIpParts(0)) & "." & CStr(arrIpParts(1)) & "." & CStr(arrIpParts(2)) & "." & CStr(arrIpParts(3))
End Function
Private Function GetTcpPortNumber(ByRef DWord As Integer) As Integer
GetTcpPortNumber = DWord / 256 + (DWord Mod 256) * 256
End Function
'================ 取的連接 =================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pdwSize As Integer
Dim iRetVal As Integer
Dim i As Integer
Dim TcpTableRow As MIB_TCPROW
Dim pStructPointer As IntPtr = IntPtr.Zero
Dim iNumberOfStructures As Integer
ListView1.Items.Clear()
iRetVal = GetTcpTable(pStructPointer, pdwSize, 0)
pStructPointer = Marshal.AllocHGlobal(pdwSize)
iRetVal = GetTcpTable(pStructPointer, pdwSize, 0)
iNumberOfStructures = Math.Ceiling((pdwSize - 4) / Marshal.SizeOf(GetType(MIB_TCPROW)))
For i = 0 To iNumberOfStructures - 1
Dim pStructPointerTemp As IntPtr = New IntPtr(pStructPointer.ToInt32() + 4 + (i * Marshal.SizeOf(GetType(MIB_TCPROW))))
TcpTableRow = New MIB_TCPROW()
With TcpTableRow
.dwLocalAddr = 0
.dwState = 0
.dwLocalPort = 0
.dwRemoteAddr = 0
.dwRemotePort = 0
End With
TcpTableRow = CType(Marshal.PtrToStructure(pStructPointerTemp, GetType(MIB_TCPROW)), MIB_TCPROW)
If Not ((CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked) And (GetIpFromLong(TcpTableRow.dwLocalAddr) = "0.0.0.0" Or GetIpFromLong(TcpTableRow.dwLocalAddr) = "127.0.0.1")) Then
With TcpTableRow
Dim itemAdd As ListViewItem
itemAdd = ListView1.Items.Add(GetIpFromLong(.dwLocalAddr))
itemAdd.SubItems.Add(CStr(GetTcpPortNumber(.dwLocalPort)))
itemAdd.SubItems.Add(GetIpFromLong(.dwRemoteAddr))
itemAdd.SubItems.Add(CStr(GetTcpPortNumber(.dwRemotePort)))
itemAdd.SubItems.Add(GetState(.dwState))
End With
End If
Next
End Sub
'================ 刪除連接 =================================
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim table As MIB_TCPROW
Dim dbste As Integer
table.dwState = MIB_TCP_STATE_DELETE_TCB
table.dwLocalAddr = Val(IPAdressBox.Text)
table.dwLocalPort = Val(LPortBox.Text)
table.dwRemoteAddr = Val(RIPBox.Text)
table.dwRemotePort = Val(RPort.Text)
dbste = SetTcpEntry(table)
If dbste <> 0 Then
MsgBox("刪除成功", vbInformation, "delete sussesful")
Else
If dbste = ERROR_ACCESS_DENIED Or dbste = 5& Then
MsgBox("限權不足", vbExclamation, "delete fail")
Else
If dbste = ERROR_NOT_SUPPORTED Or dbste = 50 Then
MsgBox("此Window版本不支援此API", vbCritical, "delete fail")
End If
End If
End If
End Sub



MIB_TCPROW.dwState TCP 目前狀態
MIB_TCPROW.dwLocalAddr IP 位置
MIB_TCPROW.dwLocalPort Port
MIB_TCPROW.dwRemoteAddr 遠程 IP 地址
MIB_TCPROW.dwRemotePort 遠程 Port

沒有留言:

張貼留言