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

📄 myclient.frm

📁 本书源码主要针对目前流行的FTP、HTTP、E-mail、Telnet、ICMP、Modem串口通信编程、拨号网络编程等内容进行详细的讲解
💻 FRM
字号:
VERSION 5.00
Begin VB.Form myclient 
   Caption         =   "TCP连接客户端程序"
   ClientHeight    =   2715
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6585
   LinkTopic       =   "Form1"
   ScaleHeight     =   2715
   ScaleWidth      =   6585
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdDisconnect 
      Caption         =   "关闭连接"
      Height          =   495
      Left            =   4680
      TabIndex        =   8
      Top             =   2040
      Width           =   1335
   End
   Begin VB.CommandButton cmdConnect 
      Caption         =   "连接服务器"
      Height          =   495
      Left            =   360
      TabIndex        =   7
      Top             =   2040
      Width           =   1335
   End
   Begin VB.TextBox txtPort 
      Height          =   405
      Left            =   4800
      TabIndex        =   6
      Text            =   "5000"
      Top             =   120
      Width           =   975
   End
   Begin VB.TextBox txtServer 
      Height          =   375
      Left            =   1200
      TabIndex        =   3
      Text            =   "10.11.111.129"
      Top             =   120
      Width           =   1575
   End
   Begin VB.CommandButton cmdSendRecv 
      Caption         =   "发送数据"
      Default         =   -1  'True
      Enabled         =   0   'False
      Height          =   495
      Left            =   2520
      Style           =   1  'Graphical
      TabIndex        =   2
      Top             =   2040
      Width           =   1335
   End
   Begin VB.TextBox txtRecv 
      Height          =   975
      Left            =   3360
      MultiLine       =   -1  'True
      TabIndex        =   1
      Top             =   960
      Width           =   3135
   End
   Begin VB.TextBox txtSend 
      Height          =   975
      Left            =   120
      MultiLine       =   -1  'True
      TabIndex        =   0
      Top             =   960
      Width           =   3015
   End
   Begin VB.Label Label4 
      Caption         =   "回显信息"
      Height          =   255
      Left            =   3480
      TabIndex        =   10
      Top             =   600
      Width           =   1095
   End
   Begin VB.Label Label3 
      Caption         =   "发送信息"
      Height          =   255
      Left            =   120
      TabIndex        =   9
      Top             =   720
      Width           =   1215
   End
   Begin VB.Label Label2 
      Caption         =   "服务器端口"
      Height          =   375
      Left            =   3600
      TabIndex        =   5
      Top             =   240
      Width           =   1215
   End
   Begin VB.Label Label1 
      Caption         =   "服务器地址"
      Height          =   255
      Left            =   120
      TabIndex        =   4
      Top             =   240
      Width           =   1335
   End
End
Attribute VB_Name = "myclient"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim soc As Long, dwRc As Long
Dim RemoteAddr As sockaddr
Dim RetMsg As String

'
' cmdSendRecv_Click事件
'
'    该事件是处理按钮“发送”单击事件
'    当用户单击该按钮时,发送文本况中的信息将被
'    发送到服务器,并读取从服务器回显的数据。
Private Sub cmdSendRecv_Click()
    If soc = INVALID_SOCKET Then
        MsgBox "Create Socket First"
        Exit Sub
    End If
    
    RetMsg = String(7000, 0)
    '
    ' send data
    dwRc = send(soc, ByVal txtSend.Text, Len(txtSend.Text), 0)
    If dwRc = SOCKET_ERROR Then
        MsgBox "Couldn't send data to remote Socket. Error: " & Err.LastDllError
    Else
        ' receive data
        dwRc = recv(soc, ByVal RetMsg, 7000, 0)
        If dwRc = SOCKET_ERROR Then
            MsgBox "Couldn't recieve data from remote Socket. Error: " & Err.LastDllError
        Else
            txtRecv.Text = Left(RetMsg, InStr(RetMsg, Chr(0)))
        End If
    End If
End Sub

'
' cmdConnect_Click事件
'    该事件是用户单击“连接”按钮的时候发生
'    当该事件发生时,客户端尝试连接服务器端
'    在连接服务器之前,首先要指定服务器地址和服务器端口号
'
Private Sub cmdConnect_Click()
        
    soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
    If soc = INVALID_SOCKET Then
        MsgBox "Couldn't Create Socket. Error: " & Err.LastDllError
    Else
        RemoteAddr.sin_family = AF_INET     ' address family, internet: 2
        RemoteAddr.sin_port = 5000 'htons(CLng(txtPort.Text))     ' port 7, must convert
        RemoteAddr.sin_addr = GetHostByNameAlias(txtServer.Text)
        
        'connect
        dwRc = connect(soc, RemoteAddr, sockaddr_size)
            
        If dwRc = SOCKET_ERROR Then
            MsgBox "Couldn't connect to remote Socket. Error: " & Err.LastDllError
        Else
            cmdConnect.Enabled = False
            cmdSendRecv.Enabled = True
            cmdDisconnect.Enabled = True
        End If

    End If
End Sub

'
' cmdDisconnect_Click事件
'
'    该事件当单击“关闭连接”的时候发生
'    当用户单击该按钮时,连接被关闭
'
Private Sub cmdDisconnect_Click()
    txtSend.Text = ""
    txtRecv.Text = ""
    If soc <> INVALID_SOCKET Then closesocket (soc)
    cmdConnect.Enabled = True
    cmdSendRecv.Enabled = False
    cmdDisconnect.Enabled = False
    soc = INVALID_SOCKET
End Sub

'
' Form_Load事件
'
'    载入窗体事件,该事件确保Winsock DLL
'    被正确地载入系统
'
Private Sub Form_Load()
    If TCPIPStartup Then
        cmdSendRecv.Enabled = True
    Else
        MsgBox "Windows Sockets not initialized. Error: " & Err.LastDllError
    End If
    soc = INVALID_SOCKET
    cmdConnect.Enabled = True
    cmdSendRecv.Enabled = False
    cmdDisconnect.Enabled = False
End Sub

'
' Form_Unload事件
'
'    卸载窗体事件,该事件中卸载Winsock DLL
'
Private Sub Form_Unload(Cancel As Integer)
    TCPIPShutDown
End Sub

⌨️ 快捷键说明

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