📄 frmoutclientserver.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmOutClientServer
Caption = "外网客户接收携带新地址端口的数据包"
ClientHeight = 6045
ClientLeft = 60
ClientTop = 450
ClientWidth = 7320
LinkTopic = "Form1"
ScaleHeight = 6045
ScaleWidth = 7320
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdConnect
Caption = "开始连接新端口"
Height = 375
Left = 3735
TabIndex = 26
Top = 3165
Width = 1560
End
Begin VB.CommandButton cmdCall
Caption = "用新端口发数据"
Height = 375
Left = 5475
TabIndex = 25
Top = 3165
Width = 1560
End
Begin VB.CommandButton cmdOk
Caption = "开始监听端口"
Height = 375
Left = 360
TabIndex = 12
Top = 3180
Width = 1560
End
Begin VB.TextBox txtService
Height = 285
Index = 6
Left = 5400
TabIndex = 11
Text = "Text1"
Top = 1260
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 5
Left = 5400
TabIndex = 10
Text = "Text1"
Top = 870
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 4
Left = 1935
TabIndex = 9
Text = "Text1"
Top = 2055
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 3
Left = 1950
TabIndex = 8
Text = "Text1"
Top = 1680
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 2
Left = 1935
TabIndex = 7
Text = "Text1"
Top = 1260
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 1
Left = 1950
TabIndex = 6
Text = "Text1"
Top = 855
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 7
Left = 5400
TabIndex = 5
Text = "Text1"
Top = 1650
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 8
Left = 5400
TabIndex = 4
Text = "Text1"
Top = 2055
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 0
Left = 1950
TabIndex = 0
Text = "Text1"
Top = 495
Width = 5025
End
Begin VB.TextBox txtService
Height = 285
Index = 9
Left = 1950
TabIndex = 3
Text = "Text1"
Top = 2685
Width = 1575
End
Begin VB.TextBox txtService
Height = 285
Index = 10
Left = 5385
TabIndex = 2
Text = "Text1"
Top = 2670
Width = 1575
End
Begin VB.TextBox txtDisplay
Height = 2235
Left = 345
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 1
Text = "frmOutClientServer.frx":0000
Top = 3720
Width = 6660
End
Begin MSWinsockLib.Winsock wskOut
Left = 2340
Top = 3180
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "请输入以下字段:"
Height = 195
Left = 330
TabIndex = 24
Top = 225
Width = 1440
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "内网客户初始端口:"
Height = 195
Index = 2
Left = 330
TabIndex = 23
Top = 1305
Width = 1620
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "内网客户新端口:"
Height = 195
Index = 4
Left = 330
TabIndex = 22
Top = 2130
Width = 1440
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "内网客户新地址:"
Height = 195
Index = 3
Left = 330
TabIndex = 21
Top = 1725
Width = 1440
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "内网客户初始地址:"
Height = 195
Index = 1
Left = 330
TabIndex = 20
Top = 900
Width = 1620
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "NAT外网初始端口:"
Height = 195
Index = 6
Left = 3690
TabIndex = 19
Top = 1305
Width = 1590
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "NAT外网新端口:"
Height = 195
Index = 8
Left = 3690
TabIndex = 18
Top = 2115
Width = 1410
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "NAT外网新地址:"
Height = 195
Index = 7
Left = 3690
TabIndex = 17
Top = 1725
Width = 1410
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "NAT外网初始地址:"
Height = 195
Index = 5
Left = 3690
TabIndex = 16
Top = 900
Width = 1590
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "所用协议:"
Height = 195
Index = 0
Left = 330
TabIndex = 15
Top = 540
Width = 900
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "外网客户端口:"
Height = 195
Index = 10
Left = 3690
TabIndex = 14
Top = 2715
Width = 1260
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "外网客户地址:"
Height = 195
Index = 9
Left = 315
TabIndex = 13
Top = 2700
Width = 1260
End
Begin VB.Line Line1
Index = 0
X1 = 345
X2 = 7065
Y1 = 2535
Y2 = 2535
End
Begin VB.Line Line1
BorderColor = &H80000004&
Index = 1
X1 = 345
X2 = 7050
Y1 = 2550
Y2 = 2550
End
Begin VB.Line Line1
Index = 2
X1 = 345
X2 = 7065
Y1 = 3045
Y2 = 3045
End
Begin VB.Line Line1
BorderColor = &H80000004&
Index = 3
X1 = 345
X2 = 7050
Y1 = 3060
Y2 = 3060
End
End
Attribute VB_Name = "frmOutClientServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private cbdFromInClient As clsBringData '数据包对象
'0.初始化文本框数据
Private Sub Form_Load()
txtService(0).Text = "TCP"
txtService(1).Text = ""
txtService(2).Text = ""
txtService(3).Text = ""
txtService(4).Text = ""
txtService(5).Text = ""
txtService(6).Text = ""
txtService(7).Text = ""
txtService(8).Text = ""
txtService(9).Text = "193.168.22.44"
txtService(10).Text = 991
txtDisplay.Text = ""
End Sub
'1.开始监听端口,以接收来自内网客户端的连接请求
Private Sub cmdOk_Click()
DisplayMsg "开始监听端口:" & txtService(10).Text
wskOut.LocalPort = txtService(10).Text
wskOut.Listen
End Sub
'1.接收来自内网客户端的连接请求
Private Sub wskOut_ConnectionRequest(ByVal requestID As Long)
'检查控件的 State 属性是否为关闭的。
'如果不是,
'在接受新的连接之前先关闭此连接。
If wskOut.State <> sckClosed Then wskOut.Close
'接受具有 requestID 参数的连接。
wskOut.Accept requestID
DisplayMsg "接收来自内网客户端的连接"
End Sub
'1.连接完成后,接收来自内网客户端的数据包
Private Sub wskOut_DataArrival(ByVal bytesTotal As Long)
Dim strTemp As String
wskOut.GetData strTemp
Set cbdFromInClient = New clsBringData
cbdFromInClient.StringToDataField strTemp
DisplayMsg "获得来自内网客户端的数据"
DisplayMsg "数据内容为:" & cbdFromInClient.strDisplay
FillData
txtService(5).Text = wskOut.RemoteHostIP
txtService(6).Text = wskOut.RemotePort
End Sub
'2.换内网客户端为服务器。连接到内网客户端,实际上是连接到NAT
Private Sub cmdConnect_Click()
DisplayMsg "连接内网客户端..."
wskOut.Close
If txtService(0).Text = "TCP" Then
wskOut.Protocol = sckTCPProtocol
Else
wskOut.Protocol = sckUDPProtocol
End If
wskOut.Connect txtService(7).Text, txtService(8).Text
End Sub
'2.如果连接到内网客户端则显示相关信息
Private Sub wskOut_Connect()
DisplayMsg "完成连接内网客户端..."
End Sub
'2.连接到内网客户端后,发送数据包
Private Sub cmdCall_Click()
DisplayMsg "开始用新地址端口发送数据..."
DisplayMsg "开始生成数据包..."
cbdFromInClient.strDisplay = "来自" & wskOut.LocalIP & ":" & wskOut.LocalPort & "的数据包。使用新地址端口发送。"
cbdFromInClient.strOldNatIP = txtService(5).Text
cbdFromInClient.strOldNatPort = txtService(6).Text
DisplayMsg "完成数据包"
DisplayMsg "发送数据包到内网客户端..."
wskOut.SendData cbdFromInClient.DataFieldToString
DisplayMsg "完成发送数据包到内网客户端"
End Sub
'根据数据包填充文本框
Private Sub FillData()
txtService(1).Text = cbdFromInClient.strOldIP
txtService(2).Text = cbdFromInClient.strOldPort
txtService(3).Text = cbdFromInClient.strNewIP
txtService(4).Text = cbdFromInClient.strNewPort
txtService(5).Text = cbdFromInClient.strOldNatIP
txtService(6).Text = cbdFromInClient.strOldNatPort
txtService(7).Text = cbdFromInClient.strNewNatIP
txtService(8).Text = cbdFromInClient.strNewNatPort
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -