📄 frmserver.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmServer
BorderStyle = 1 'Fixed Single
Caption = "TCP服务器"
ClientHeight = 2490
ClientLeft = 45
ClientTop = 330
ClientWidth = 7815
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2490
ScaleMode = 0 'User
ScaleWidth = 7939.048
StartUpPosition = 3 '窗口缺省
Begin VB.ComboBox cboClient
Height = 300
Left = 840
Style = 2 'Dropdown List
TabIndex = 0
Top = 2040
Width = 975
End
Begin VB.CommandButton comdSendData
Caption = "发送"
Height = 300
Left = 6480
TabIndex = 3
Top = 2040
Width = 1215
End
Begin VB.TextBox txtSendData
Height = 300
Left = 2520
TabIndex = 2
Top = 2040
Width = 3855
End
Begin MSWinsockLib.Winsock tcpServer
Index = 0
Left = 6600
Top = 960
_ExtentX = 741
_ExtentY = 741
_Version = 393216
RemoteHost = "IBMCSMY"
End
Begin VB.TextBox txtOutput
Height = 1500
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 6
Top = 360
Width = 7575
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "发送到:"
Height = 180
Left = 120
TabIndex = 5
Top = 2040
Width = 630
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "接收数据:"
Height = 180
Left = 120
TabIndex = 4
Top = 120
Width = 810
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "数据:"
Height = 180
Left = 2040
TabIndex = 1
Top = 2040
Width = 450
End
End
Attribute VB_Name = "frmServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim intNum As Long
Private Sub Form_Load()
intNum = 0
tcpServer(0).LocalPort = 1001 '设置本地端口
tcpServer(0).Listen '启动监听
End Sub
Private Sub tcpServer_Close(Index As Integer)
tcpServer(Index).Close '关闭连接
Unload tcpServer(Index) '卸载控件实例
Dim I As Integer
For I = 0 To cboClient.ListCount - 1
If cboClient.List(I) = Index Then
cboClient.RemoveItem I '从组合框移除客户端序号
Exit For
End If
Next
End Sub
Private Sub tcpServer_ConnectionRequest _
(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
intNum = intNum + 1
Load tcpServer(intNum) '创建新的连接控件实例
tcpServer(intNum).LocalPort = 0 '设置本地随机端口
tcpServer(intNum).Accept requestID '建立新的远程连接
cboClient.AddItem intNum '添加客户端序号到组合框
End If
End Sub
Private Sub comdSendData_Click()
'向远程计算机发送数据
On Error GoTo ErrBar
tcpServer(Val(cboClient.Text)).SendData txtSendData
Exit Sub
ErrBar:
MsgBox Error & Space(2) & Err
End Sub
Private Sub tcpServer_DataArrival(Index As Integer, _
ByVal bytesTotal As Long)
'接收远程计算机传送的数据
Dim strData As String
tcpServer(Index).GetData strData
txtOutput = txtOutput & Chr(13) & Chr(10) _
& Index & "号:" & strData
txtOutput.SelStart = Len(txtOutput)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -