⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmupnptraversalbringin.frm

📁 对NAT有详细的总结分析
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmUPnPTraversalBringIn 
   Caption         =   "内网客户携带新地址端口穿越NAT"
   ClientHeight    =   6270
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7365
   LinkTopic       =   "Form2"
   ScaleHeight     =   6270
   ScaleWidth      =   7365
   StartUpPosition =   1  'CenterOwner
   Begin VB.CommandButton cmdConnect 
      Caption         =   "开始连接"
      Height          =   375
      Left            =   3795
      TabIndex        =   26
      Top             =   3135
      Width           =   1560
   End
   Begin VB.TextBox txtDisplay 
      Height          =   2520
      Left            =   375
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   25
      Text            =   "frmUPnPTraversalBringIn.frx":0000
      Top             =   3675
      Width           =   6660
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   10
      Left            =   5415
      TabIndex        =   24
      Text            =   "Text1"
      Top             =   2625
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   9
      Left            =   1980
      TabIndex        =   23
      Text            =   "Text1"
      Top             =   2640
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   0
      Left            =   1965
      TabIndex        =   20
      Text            =   "Text1"
      Top             =   450
      Width           =   5055
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   8
      Left            =   5430
      TabIndex        =   18
      Text            =   "Text1"
      Top             =   2010
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   7
      Left            =   5430
      TabIndex        =   17
      Text            =   "Text1"
      Top             =   1605
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   1
      Left            =   1980
      TabIndex        =   7
      Text            =   "Text1"
      Top             =   810
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   2
      Left            =   1965
      TabIndex        =   6
      Text            =   "Text1"
      Top             =   1215
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   3
      Left            =   1980
      TabIndex        =   5
      Text            =   "Text1"
      Top             =   1635
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   4
      Left            =   1965
      TabIndex        =   4
      Text            =   "Text1"
      Top             =   2010
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   5
      Left            =   5430
      TabIndex        =   3
      Text            =   "Text1"
      Top             =   825
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   6
      Left            =   5430
      TabIndex        =   2
      Text            =   "Text1"
      Top             =   1215
      Width           =   1575
   End
   Begin VB.CommandButton cmdQuery 
      Caption         =   "获取NAT外网信息"
      Height          =   375
      Left            =   330
      TabIndex        =   1
      Top             =   3135
      Width           =   1560
   End
   Begin VB.CommandButton cmdOk 
      Caption         =   "开始穿越"
      Height          =   375
      Left            =   5475
      TabIndex        =   0
      Top             =   3135
      Width           =   1560
   End
   Begin MSWinsockLib.Winsock wskIn 
      Left            =   2370
      Top             =   3135
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.Line Line1 
      BorderColor     =   &H80000004&
      Index           =   3
      X1              =   375
      X2              =   7080
      Y1              =   3015
      Y2              =   3015
   End
   Begin VB.Line Line1 
      Index           =   2
      X1              =   375
      X2              =   7095
      Y1              =   3000
      Y2              =   3000
   End
   Begin VB.Line Line1 
      BorderColor     =   &H80000004&
      Index           =   1
      X1              =   375
      X2              =   7080
      Y1              =   2505
      Y2              =   2505
   End
   Begin VB.Line Line1 
      Index           =   0
      X1              =   375
      X2              =   7095
      Y1              =   2490
      Y2              =   2490
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "外网客户地址:"
      Height          =   195
      Index           =   9
      Left            =   345
      TabIndex        =   22
      Top             =   2655
      Width           =   1260
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "外网客户端口:"
      Height          =   195
      Index           =   10
      Left            =   3720
      TabIndex        =   21
      Top             =   2670
      Width           =   1260
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "所用协议:"
      Height          =   195
      Index           =   0
      Left            =   360
      TabIndex        =   19
      Top             =   495
      Width           =   900
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网初始地址:"
      Height          =   195
      Index           =   5
      Left            =   3720
      TabIndex        =   16
      Top             =   855
      Width           =   1590
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网新地址:"
      Height          =   195
      Index           =   7
      Left            =   3720
      TabIndex        =   15
      Top             =   1680
      Width           =   1410
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网新端口:"
      Height          =   195
      Index           =   8
      Left            =   3720
      TabIndex        =   14
      Top             =   2070
      Width           =   1410
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网初始端口:"
      Height          =   195
      Index           =   6
      Left            =   3720
      TabIndex        =   13
      Top             =   1260
      Width           =   1590
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网客户初始地址:"
      Height          =   195
      Index           =   1
      Left            =   360
      TabIndex        =   12
      Top             =   855
      Width           =   1620
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网客户新地址:"
      Height          =   195
      Index           =   3
      Left            =   360
      TabIndex        =   11
      Top             =   1680
      Width           =   1440
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网客户新端口:"
      Height          =   195
      Index           =   4
      Left            =   360
      TabIndex        =   10
      Top             =   2085
      Width           =   1440
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网客户初始端口:"
      Height          =   195
      Index           =   2
      Left            =   360
      TabIndex        =   9
      Top             =   1260
      Width           =   1620
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "请输入以下字段:"
      Height          =   195
      Left            =   360
      TabIndex        =   8
      Top             =   180
      Width           =   1440
   End
End
Attribute VB_Name = "frmUPnPTraversalBringIn"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private cbdFromOutClient As clsBringData '数据包对象

'0.初始化文本框数据
Private Sub Form_Load()
    txtService(0).Text = "TCP"
    txtService(1).Text = wskIn.LocalIP
    txtService(2).Text = "0"
    txtService(3).Text = wskIn.LocalIP
    txtService(4).Text = 882
    txtService(5).Text = "0"
    txtService(6).Text = "0"
    txtService(7).Text = "0"
    txtService(8).Text = 6882
    txtService(9).Text = "193.168.22.44"
    txtService(10).Text = 991
    
    txtDisplay.Text = ""
End Sub

'0.1.获取NAT的外网地址
Private Sub cmdQuery_Click()
    txtService(5).Text = GetExternalIP()
    txtService(7).Text = txtService(5).Text
End Sub

'1.连接外网客户端
Private Sub cmdConnect_Click()
    If CheckInput() = False Then
        Exit Sub
    End If
    
    DisplayMsg "连接外网客户端..."
    wskIn.Close
    If txtService(0).Text = "TCP" Then
        wskIn.Protocol = sckTCPProtocol
    Else
        wskIn.Protocol = sckUDPProtocol
    End If
    wskIn.Connect txtService(9).Text, txtService(10).Text
    
End Sub

'1.如果连接完成,则显示相关信息
Private Sub wskIn_Connect()
    DisplayMsg "完成连接外网客户端"
    txtService(2).Text = wskIn.LocalPort
    DisplayMsg "更新内网客户初始端口"
End Sub

'1.连接完成后,发送数据,并设置NAT以让新端口数据能穿越NAT
Private Sub cmdOK_Click()
    If CheckInput() = False Then
        Exit Sub
    End If
    
    DisplayMsg "开始穿越NAT..."
    
    DisplayMsg "开始生成携带新地址端口的数据包..."
    Dim cbdData As New clsBringData
    cbdData.strDisplay = "来自" & txtService(1).Text & ":" & txtService(2).Text & "的数据包,请通过新地址端口回话,谢谢"
    cbdData.strOldIP = txtService(1).Text
    cbdData.strOldPort = txtService(2).Text
    cbdData.strNewIP = txtService(3).Text
    cbdData.strNewPort = txtService(4).Text
    cbdData.strOldNatIP = txtService(5).Text
    cbdData.strOldNatPort = txtService(6).Text
    cbdData.strNewNatIP = txtService(7).Text
    cbdData.strNewNatPort = txtService(8).Text
    DisplayMsg "完成携带新地址端口的数据包"
    
    DisplayMsg "正在设置NAT,穿越中..."
    If SetExternalPort(txtService(8).Text, txtService(0).Text, txtService(4).Text, wskIn.LocalIP, True, Me.Caption + ":" + txtService(9).Text) = True Then
        DisplayMsg "设置NAT成功,穿越成功。外网用户通过" + txtService(7).Text + ":" + txtService(8).Text + "可以访问内网新端口了"
    Else
        DisplayMsg "设置NAT失败,穿越失败。"
    End If
    
    DisplayMsg "发送数据包到外网客户端..."
    wskIn.SendData cbdData.DataFieldToString
End Sub

'1.数据发送完成后显示相关信息,并开始做服务器,监听端口等待外网客户端的连接请求
Private Sub wskIn_SendComplete()
    DisplayMsg "完成发送数据包到外网客户端"
    
    DisplayMsg "开始监听端口:" & txtService(4).Text
    wskIn.Close
    wskIn.LocalPort = txtService(4).Text
    wskIn.Listen
End Sub

'2.接收外网客户端的连接请求
Private Sub wskIn_ConnectionRequest(ByVal requestID As Long)
    '检查控件的 State 属性是否为关闭的。
    '如果不是,
    '在接受新的连接之前先关闭此连接。
    If wskIn.State <> sckClosed Then wskIn.Close
    '接受具有 requestID 参数的连接。
    wskIn.Accept requestID
    DisplayMsg "接收来自外网客户的连接"
End Sub

'2.接收来自外网客户端的数据包,并根据数据包显示相关信息
Private Sub wskIn_DataArrival(ByVal bytesTotal As Long)
    Dim strTemp As String
    wskIn.GetData strTemp
    Set cbdFromOutClient = New clsBringData
    cbdFromOutClient.StringToDataField strTemp
    DisplayMsg "获得来自外网客户端的数据"
    DisplayMsg "数据内容为:" & cbdFromOutClient.strDisplay
        
    txtService(5).Text = cbdFromOutClient.strOldNatIP
    txtService(6).Text = cbdFromOutClient.strOldNatPort
    DisplayMsg "更新NAT外网初始端口"
End Sub

'改变文本框的底色
Private Sub txtService_GotFocus(Index As Integer)
    txtService(Index).BackColor = &H80000018
End Sub

'改变文本框的底色
Private Sub txtService_LostFocus(Index As Integer)
    txtService(Index).BackColor = vbWhite
End Sub

'处理文本框中按下回车键
Private Sub txtService_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim intTemp As Integer
    If KeyAscii = 13 Then
        If Index = txtService.UBound Then
            intTemp = txtService.LBound
        Else
            intTemp = Index + 1
        End If
        txtService(intTemp).SetFocus
        KeyAscii = 0
    End If
End Sub

'显示信息
Private Sub DisplayMsg(strMsg As String)
    txtDisplay.Text = txtDisplay.Text & DateTime.Now & " --> " & strMsg & Chr(13) & Chr(10)
    txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

'检查输入
Private Function CheckInput() As Boolean
    CheckInput = True
    Dim i As Integer
    For i = txtService.LBound To txtService.UBound
        If txtService(i).Text = "" Then
            MsgBox "请输入数据到" & Label1(i).Caption
            txtService(i).SetFocus
            CheckInput = False
            Exit Function
        End If
    Next i
    txtService(0).Text = UCase(txtService(0).Text)
    If txtService(0).Text <> "TCP" And txtService(0).Text <> "UDP" Then
        MsgBox "请输入正确数据到" & Label1(0).Caption & "如:TCP、UDP"
        txtService(0).SetFocus
        CheckInput = False
        Exit Function
    End If
'    其他的情况目前没有检查,如IP地址合法性、端口合法性等
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -