📄 frmtcpclient.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmTCPClient
Caption = "Simple TCP Client"
ClientHeight = 3750
ClientLeft = 60
ClientTop = 345
ClientWidth = 4710
LinkTopic = "Form1"
ScaleHeight = 3750
ScaleWidth = 4710
StartUpPosition = 3 'Windows Default
Begin MSWinsockLib.Winsock tcpClient
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 = "frmTCPClient"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Fig. 19.12
' A simple client using TCP sockets
Option Explicit
Private Sub Form_Load()
cmdSend.Enabled = False
' set up local port and wait for connection
tcpClient.RemoteHost = _
InputBox("Enter the remote host IP address", _
"IP Address", "localhost")
If tcpClient.RemoteHost = "" Then
tcpClient.RemoteHost = "localhost"
End If
tcpClient.RemotePort = 5000 ' server port
Call tcpClient.Connect ' connect to RemoteHost address
End Sub
Private Sub Form_Terminate()
Call tcpClient.Close
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 tcpClient_Connect()
' when connection occurs, display a message
cmdSend.Enabled = True
txtOutput.Text = "Connected to IP Address: " & _
tcpClient.RemoteHostIP & vbCrLf & "Port #: " & _
tcpClient.RemotePort & vbCrLf & vbCrLf
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim message As String
Call tcpClient.GetData(message) ' get data from server
txtOutput.Text = txtOutput.Text & message & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Close()
cmdSend.Enabled = False
Call tcpClient.Close ' server closed, client should too
txtOutput.Text = _
txtOutput.Text & "Server closed connection." & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_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 server
Call tcpClient.SendData("CLIENT >>> " & txtSend.Text)
txtOutput.Text = txtOutput.Text & _
"CLIENT >>> " & txtSend.Text & vbCrLf & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
txtSend.Text = ""
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -