📄 winsockserver.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form ServerFrm
AutoRedraw = -1 'True
Caption = "Server"
ClientHeight = 3780
ClientLeft = 3795
ClientTop = 1905
ClientWidth = 4080
LinkTopic = "Form1"
ScaleHeight = 3780
ScaleWidth = 4080
Tag = "hello"
Begin VB.TextBox Text2
Height = 855
Left = 120
MultiLine = -1 'True
TabIndex = 3
Top = 2520
Width = 3855
End
Begin VB.CommandButton Command2
Caption = "退出"
Height = 255
Left = 2520
TabIndex = 2
Top = 3480
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "发送"
Height = 255
Left = 360
TabIndex = 1
Top = 3480
Width = 1215
End
Begin VB.TextBox Text1
Height = 2295
Left = 120
Locked = -1 'True
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 0
Top = 0
Width = 3855
End
Begin MSWinsockLib.Winsock sockserver
Left = 1800
Top = 3360
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
End
Attribute VB_Name = "ServerFrm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim SendMsg As String
Private Sub Form_Load()
sockserver.LocalPort = 2000 '服务器端口号,最好大于1000
sockserver.Listen '开始侦听
Text1.Locked = True
End Sub
' 建立新连接
Private Sub SockServer_ConnectionRequest(ByVal requestID As Long)
If sockserver.State <> sckClosed Then sockserver.Close
sockserver.Accept requestID '表示客户请求连接的ID号
SendMsg = " You have connected to server!"
sockserver.SendData SendMsg
End Sub
' 当客户向服务器发送数据到达后 , 产生DataArrival事件, 在事件中接收数据, GetData方法接收数据?
Private Sub SockServer_DataArrival(ByVal bytesTotal As Long)
Dim s As String
sockserver.GetData s
If Text1.Text = "" Then
Text1.Text = "From Client:" & s
Else
Text1.Text = Text1.Text & vbCrLf & "From Client:" & s
End If
End Sub
' 当需要向客户发送数据时,只需调用SendData方法。
Private Sub Command1_Click()
SendMsg = Text2.Text
sockserver.SendData SendMsg
End Sub
' 发送完毕后回显
Private Sub sockserver_SendComplete()
If Text1.Text = "" Then
Text1.Text = "To Client:" & SendMsg
Else
Text1.Text = Text1.Text & vbCrLf & "To Client:" & SendMsg
End If
End Sub
Private Sub Command2_Click()
sockserver.Close
End
End Sub
Private Sub Form_Unload(Cancel As Integer)
sockserver.Close
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -