📄 frmudp.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1
Caption = "WinSock控件UDP"
ClientHeight = 6375
ClientLeft = 60
ClientTop = 345
ClientWidth = 7680
LinkTopic = "Form1"
ScaleHeight = 6375
ScaleWidth = 7680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdRemoteSet
Caption = "远程设置"
Enabled = 0 'False
Height = 375
Left = 6240
TabIndex = 15
Top = 2400
Width = 1095
End
Begin VB.Frame fraRemote
Caption = "本地机配置:"
Height = 1455
Left = 5400
TabIndex = 12
Top = 3000
Width = 2055
Begin VB.CommandButton cmdLocalSet
Caption = "本地配置"
Height = 375
Left = 840
TabIndex = 16
Top = 960
Width = 1095
End
Begin VB.TextBox txtLocalPort
Height = 285
Left = 480
TabIndex = 13
Top = 495
Width = 1455
End
Begin VB.Label Label1
Caption = "输入本地端口:"
Height = 255
Left = 120
TabIndex = 14
Top = 240
Width = 1335
End
End
Begin VB.TextBox txtChat
Height = 4335
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 10
Top = 480
Width = 5175
End
Begin VB.Frame fraInput
Caption = "交谈输入区:"
Height = 1215
Left = 120
TabIndex = 5
Top = 4920
Width = 7215
Begin VB.TextBox txtInput
Height = 375
Left = 120
TabIndex = 8
Top = 600
Width = 5535
End
Begin VB.CommandButton cmdClear
Caption = "清空交谈内容"
Height = 375
Left = 5760
TabIndex = 7
Top = 120
Width = 1335
End
Begin VB.CommandButton cmdSend
Caption = "发送数据"
Height = 375
Left = 5760
TabIndex = 6
Top = 600
Width = 1335
End
Begin VB.Label Label8
Caption = "输入:"
Height = 255
Left = 120
TabIndex = 9
Top = 360
Width = 615
End
End
Begin VB.Frame fraLocal
Caption = "远程机配置:"
Height = 2415
Left = 5400
TabIndex = 0
Top = 480
Width = 2055
Begin VB.TextBox txtHost
Height = 315
Left = 480
TabIndex = 2
Top = 680
Width = 1455
End
Begin VB.TextBox txtRemotePort
Height = 285
Left = 480
TabIndex = 1
Top = 1340
Width = 1455
End
Begin VB.Label Label5
Caption = "输入主机名:"
Height = 255
Left = 240
TabIndex = 4
Top = 400
Width = 1215
End
Begin VB.Label Label6
Caption = "输入远程端口:"
Height = 255
Left = 240
TabIndex = 3
Top = 1080
Width = 1335
End
End
Begin MSWinsockLib.Winsock WinsockUdp
Left = 7080
Top = 0
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.Label Label9
Caption = "交谈显示窗口:"
Height = 255
Left = 120
TabIndex = 11
Top = 240
Width = 1455
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim HostName As String * 256 '远程计算机名称
Dim LHostName As String * 256 '本机主机名称
Dim rPort As Long '远程端口
Dim lPort As Long '本地端口
Private Sub cmdClear_Click()
txtChat.Text = vbNullString
End Sub
Private Sub cmdLocalSet_Click()
If Trim$(txtLocalPort.Text) <> vbNullString Then
lPort = CLng(Trim$(txtLocalPort.Text))
'设置本地端口,同时开启远程设置功能
WinsockUdp.localPort = lPort
cmdRemoteSet.Enabled = True
Else
MsgBox "每个设置项不能为空!"
End If
End Sub
Private Sub cmdSend_Click()
Dim sData As String
sData = Trim$(LHostName) & ":" & Trim$(txtInput.Text)
On Error GoTo err:
WinsockUdp.SendData sData
'UDP传送数据并不能确保数据的正确送到,在此将自己的每次输入
'都显示在交谈窗口
txtChat.Text = Trim$(txtChat.Text) & sData & vbCrLf
Exit Sub
err:
MsgBox err.Description
End Sub
Private Sub cmdRemoteSet_Click()
If Trim$(txtHost.Text) <> vbNullString Or _
Trim$(txtRemotePort.Text) <> vbNullString Then
rPort = CLng(Trim$(txtRemotePort.Text))
HostName = Trim$(txtHost.Text)
'设置远程端口和远程主机名
WinsockUdp.RemotePort = rPort
WinsockUdp.RemoteHost = Trim$(HostName)
'绑定本地端口
WinsockUdp.Bind lPort
Else
MsgBox "每个设置项不能为空!"
End If
End Sub
Private Sub Form_Load()
'设置好为UDP协议
WinsockUdp.Protocol = sckUDPProtocol
LHostName = WinsockUdp.LocalHostName
End Sub
Private Sub WinsockUdp_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
On Error Resume Next
WinsockUdp.GetData sData
'如果传送的数据不是空字符,那么就显示在交谈窗口
If Trim$(sData) <> vbNullString Then
txtChat.Text = Trim$(txtChat.Text) & sData & vbCrLf
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -