【VB开源代码栏目提醒】:本文主要为网学会员提供frmServer.frm,希望对需要frmServer.frm网友有所帮助,学习一下!
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmServer
Caption = "TCP服务器"
ClientHeight = 2160
ClientLeft = 60
ClientTop = 450
ClientWidth = 4470
LinkTopic = "Form1"
ScaleHeight = 2160
ScaleWidth = 4470
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdSend
Caption = "发送"
Height = 375
Left = 3120
TabIndex = 4
Top = 1680
Width = 855
End
Begin MSWinsockLib.Winsock tcpServer
Left = 360
Top = 1680
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.TextBox txtOutput
Height = 375
Left = 1320
TabIndex = 3
Top = 1080
Width = 2895
End
Begin VB.TextBox txtSendData
Height = 375
Left = 1320
TabIndex = 2
Top = 360
Width = 2895
End
Begin VB.Label Label2
Caption = "接收的信息"
Height = 255
Left = 240
TabIndex = 1
Top = 1200
Width = 975
End
Begin
VB.Label Label1
Caption = "发送的信息"
Height = 255
Left = 240
TabIndex = 0
Top = 480
Width = 975
End
End
Attribute VB_Name = "frmServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdSend_Click()
'名为 txtSendData 的 TextBox 控件中
'包含了要发送的数据。当用户往文本框中
'键入数据后打击此按钮,使用 SendData 方法发送输入的字符串。
tcpServer.SendData txtSendData.Text
End Sub
Private Sub Form_Load()
'设定端口,原则上可以随意取,但是不要取一些默认的端口,如21、80等
tcpServer.LocalPort = 1001
tcpServer.Listen '侦听
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
'检查控件的 State 属性是否为关闭的。
'如果不是,
'在接受新的连接之前先关闭此连接。
If tcpServer.State <> sckClosed Then tcpServer.Close
'接受具有 requestID 参数的连接。
tcpServer.Accept requestID '接受后,即可与客户端建立连接
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
'为进入的数据声明一个变量。
'调用 GetData 方法,并将数据赋予名为 txtOutput
'的 TextBox 的 Text 属性。
Dim strData As String
tcpServer.GetData strData '获取数据
txtOutput.Text = strData '将数据显示在文本框中
End Sub
Private Sub tcpServer_Close()
tcpServer.Close '关闭TCP连接或侦听套接字
End Sub