⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmtcpclient.frm

📁 这个文件是有关系统串口通信的实例
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmTcpClient 
   Caption         =   "客户端"
   ClientHeight    =   6525
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7650
   LinkTopic       =   "Form1"
   ScaleHeight     =   6525
   ScaleWidth      =   7650
   StartUpPosition =   3  'Windows Default
   Begin VB.Frame fraTCPClient 
      Caption         =   "客户端配置:"
      Height          =   2775
      Left            =   5400
      TabIndex        =   6
      Top             =   360
      Width           =   2055
      Begin VB.TextBox txtRemotePort 
         Height          =   285
         Left            =   480
         TabIndex        =   10
         Top             =   1340
         Width           =   1455
      End
      Begin VB.CommandButton cmdTcpClose 
         Caption         =   "关闭连接"
         Height          =   375
         Left            =   600
         TabIndex        =   9
         Top             =   2280
         Width           =   1335
      End
      Begin VB.CommandButton cmdTcpCnn 
         Caption         =   "连接主机"
         Height          =   375
         Left            =   600
         TabIndex        =   8
         Top             =   1800
         Width           =   1335
      End
      Begin VB.TextBox txtTcpHost 
         Height          =   315
         Left            =   480
         TabIndex        =   7
         Top             =   680
         Width           =   1455
      End
      Begin VB.Label Label6 
         Caption         =   "输入远程端口:"
         Height          =   255
         Left            =   240
         TabIndex        =   12
         Top             =   1080
         Width           =   1335
      End
      Begin VB.Label Label5 
         Caption         =   "输入主机名:"
         Height          =   255
         Left            =   240
         TabIndex        =   11
         Top             =   400
         Width           =   1215
      End
   End
   Begin VB.Frame fraClientInput 
      Caption         =   "交谈输入区:"
      Height          =   1215
      Left            =   120
      TabIndex        =   1
      Top             =   4800
      Width           =   7215
      Begin VB.CommandButton cmdClientSend 
         Caption         =   "发送数据"
         Height          =   375
         Left            =   5760
         TabIndex        =   4
         Top             =   600
         Width           =   1335
      End
      Begin VB.CommandButton cmdClientClear 
         Caption         =   "清空交谈内容"
         Height          =   375
         Left            =   5760
         TabIndex        =   3
         Top             =   120
         Width           =   1335
      End
      Begin VB.TextBox txtClientInput 
         Height          =   375
         Left            =   120
         TabIndex        =   2
         Top             =   600
         Width           =   5535
      End
      Begin VB.Label Label8 
         Caption         =   "输入:"
         Height          =   255
         Left            =   120
         TabIndex        =   5
         Top             =   360
         Width           =   615
      End
   End
   Begin VB.TextBox txtClientChat 
      Height          =   4335
      Left            =   120
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   0
      Top             =   360
      Width           =   5175
   End
   Begin MSWinsockLib.Winsock WinsockTcpClient 
      Left            =   6960
      Top             =   3480
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin MSComctlLib.StatusBar staTcpCnn 
      Height          =   375
      Left            =   0
      TabIndex        =   13
      Top             =   6120
      Width           =   7455
      _ExtentX        =   13150
      _ExtentY        =   661
      _Version        =   393216
      BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
         NumPanels       =   2
         BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            AutoSize        =   1
            Object.Width           =   10530
         EndProperty
         BeginProperty Panel2 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
         EndProperty
      EndProperty
   End
   Begin VB.Label Label9 
      Caption         =   "交谈显示窗口:"
      Height          =   255
      Left            =   120
      TabIndex        =   14
      Top             =   120
      Width           =   1455
   End
End
Attribute VB_Name = "frmTcpClient"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim HostName As String * 256
Dim HostIP As String

Private Sub cmdTcpCnn_Click()
    If Trim$(txtTcpHost.Text) = vbNullString Or Trim$(txtRemotePort.Text) = vbNullString Then
        MsgBox "请输入远程主机名和端口!"
        Exit Sub
    End If
    WinsockTcpClient.RemoteHost = Trim$(txtTcpHost.Text)
    WinsockTcpClient.RemotePort = CLng(Trim$(txtRemotePort.Text))
    
    WinsockTcpClient.Connect
End Sub

Private Sub cmdTcpClose_Click()
    On Error GoTo err
    '断开与服务器的连接
    WinsockTcpClient.Close
    staTcpCnn.Panels(1).Text = "已经退出系统!"
    Exit Sub
err:
    MsgBox err.Description
End Sub

Private Sub cmdClientClear_Click()
    txtClientChat.Text = vbNullString
End Sub

Private Sub cmdClientSend_Click()
    On Error GoTo err
    Dim sSend As String
    '发送用户输入的文本
    sSend = Trim$(HostName) & ":" & Trim$(txtClientInput.Text) & vbLf
    WinsockTcpClient.SendData sSend
    Exit Sub
err:
    MsgBox err.Description
End Sub

Private Sub Form_Unload(Cancel As Integer)
    WinsockTcpClient.Close
End Sub

Private Sub WinsockTcpClient_Close()
    WinsockTcpClient.Close
    staTcpCnn.Panels(1).Text = "服务器关闭......"
End Sub

Private Sub WinsockTcpClient_Connect()
    '如果连接成功,那么文本框可用
    txtClientChat.Enabled = True
    txtClientInput.Enabled = True
    staTcpCnn.Panels(1).Text = "连接成功!"
End Sub

Private Sub WinsockTcpClient_DataArrival(ByVal bytesTotal As Long)
    '该事件用来接收从服务器返回的数据,并显示
    On Error GoTo err
    Dim sData As String
    WinsockTcpClient.GetData sData
    txtClientChat.Text = txtClientChat.Text & sData & vbCrLf
    Exit Sub
err:
    MsgBox err.Description
End Sub

Private Sub Form_Load()
    '获取本地计算机的名称和IP,用来判断服务器还是客户机
    HostName = WinsockTcpClient.LocalHostName
    HostIP = WinsockTcpClient.LocalIP
    '设置为TCP协议,默认方式就是TCP
    WinsockTcpClient.Protocol = sckTCPProtocol
    
    txtClientChat.Enabled = False
    txtClientInput.Enabled = False
    
    '设置默认连接的服务器名和端口
    txtRemotePort.Text = 1001
    txtTcpHost.Text = "GXP"
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -