📄 frmtcpserver.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmTCPServer
Caption = "Simple TCP Server"
ClientHeight = 3750
ClientLeft = 60
ClientTop = 345
ClientWidth = 4710
LinkTopic = "Form1"
ScaleHeight = 3750
ScaleWidth = 4710
StartUpPosition = 3 'Windows Default
Begin MSWinsockLib.Winsock tcpServer
Left = 2160
Top = 1680
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.TextBox txtOutput
Enabled = 0 'False
Height = 3375
Left = 0
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 2
Top = 360
Width = 4695
End
Begin VB.CommandButton cmdSend
Caption = "Send Text"
Height = 285
Left = 3480
TabIndex = 1
Top = 0
Width = 1215
End
Begin VB.TextBox txtSend
Height = 285
Left = 0
TabIndex = 0
Top = 0
Width = 3375
End
End
Attribute VB_Name = "frmTCPServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Fig. 19.10
' A simple server using TCP sockets
Option Explicit
Private Sub Form_Load()
cmdSend.Enabled = False
' set up local port and wait for connection
tcpServer.LocalPort = 5000
Call tcpServer.Listen
End Sub
Private Sub Form_Resize()
On Error Resume Next
Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0)
Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width)
Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _
ScaleHeight - txtSend.Height)
End Sub
Private Sub Form_Terminate()
Call tcpServer.Close
End Sub
Private Sub tcpServer_ConnectionRequest( _
ByVal requestID As Long)
' Ensure that tcpServer is closed
' before accepting a new connection
If tcpServer.State <> sckClosed Then
Call tcpServer.Close
End If
cmdSend.Enabled = True
Call tcpServer.Accept(requestID) ' accept connection
txtOutput.Text = _
"Connection from IP address: " & _
tcpServer.RemoteHostIP & vbCrLf & _
"Port #: " & tcpServer.RemotePort & vbCrLf & vbCrLf
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim message As String
Call tcpServer.GetData(message) ' get data from client
txtOutput.Text = _
txtOutput.Text & message & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpServer_Close()
cmdSend.Enabled = False
Call tcpServer.Close ' client closed, server should too
txtOutput.Text = txtOutput.Text & _
"Client closed connection." & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
Call tcpServer.Listen ' listen for next connection
End Sub
Private Sub tcpServer_Error(ByVal Number As Integer, _
Description As String, ByVal Scode As Long, _
ByVal Source As String, ByVal HelpFile As String, _
ByVal HelpContext As Long, CancelDisplay As Boolean)
Dim result As Integer
result = MsgBox(Source & ": " & Description, _
vbOKOnly, "TCP/IP Error")
End
End Sub
Private Sub cmdSend_Click()
' send data to the client
Call tcpServer.SendData("SERVER >>> " & txtSend.Text)
txtOutput.Text = txtOutput.Text & _
"SERVER >>> " & txtSend.Text & vbCrLf & vbCrLf
txtSend.Text = ""
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -