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

📄 myudp.frm

📁 本书源码主要针对目前流行的FTP、HTTP、E-mail、Telnet、ICMP、Modem串口通信编程、拨号网络编程等内容进行详细的讲解
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form udp 
   Caption         =   "Winsock控件UDP程序开发"
   ClientHeight    =   5055
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6060
   LinkTopic       =   "Form1"
   ScaleHeight     =   5055
   ScaleWidth      =   6060
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdExit 
      Caption         =   "退出程序"
      Height          =   375
      Left            =   2160
      TabIndex        =   22
      Top             =   4560
      Width           =   1455
   End
   Begin VB.Frame Frame3 
      Caption         =   "Winsock Information"
      Height          =   1575
      Left            =   6120
      TabIndex        =   17
      Top             =   2400
      Width           =   15
      Begin VB.Timer Timer1 
         Left            =   2040
         Top             =   3120
      End
      Begin VB.Label lblRemotePort 
         Height          =   255
         Left            =   1320
         TabIndex        =   30
         Top             =   2640
         Width           =   1455
      End
      Begin VB.Label Label12 
         Caption         =   " Remote Port:"
         Height          =   255
         Left            =   240
         TabIndex        =   29
         Top             =   2640
         Width           =   975
      End
      Begin VB.Label lblRemoteIP 
         Height          =   255
         Left            =   1320
         TabIndex        =   28
         Top             =   2280
         Width           =   1455
      End
      Begin VB.Label Label11 
         Caption         =   " Remote IP:"
         Height          =   255
         Left            =   360
         TabIndex        =   27
         Top             =   2280
         Width           =   855
      End
      Begin VB.Label lblReceiverState 
         Caption         =   "sckClosed"
         Height          =   255
         Left            =   1320
         TabIndex        =   26
         Top             =   1440
         Width           =   1455
      End
      Begin VB.Label lblSenderState 
         Caption         =   "sckClosed"
         Height          =   255
         Left            =   1320
         TabIndex        =   25
         Top             =   1080
         Width           =   1455
      End
      Begin VB.Label Label10 
         Caption         =   "Receiver State:"
         Height          =   255
         Left            =   120
         TabIndex        =   24
         Top             =   1440
         Width           =   1215
      End
      Begin VB.Label Label9 
         Caption         =   "Sender State:"
         Height          =   255
         Left            =   240
         TabIndex        =   23
         Top             =   1080
         Width           =   1095
      End
      Begin VB.Label lblLocalIP 
         Height          =   255
         Left            =   1320
         TabIndex        =   21
         Top             =   720
         Width           =   1335
      End
      Begin VB.Label Label8 
         Caption         =   "Local IP:"
         Height          =   255
         Left            =   600
         TabIndex        =   20
         Top             =   720
         Width           =   735
      End
      Begin VB.Label lblHostName 
         Height          =   255
         Left            =   1320
         TabIndex        =   19
         Top             =   360
         Width           =   1455
      End
      Begin VB.Label Label7 
         Caption         =   " Local Name:"
         Height          =   255
         Left            =   240
         TabIndex        =   18
         Top             =   360
         Width           =   975
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "接收端"
      Height          =   2295
      Left            =   120
      TabIndex        =   5
      Top             =   2160
      Width           =   5895
      Begin VB.CommandButton cmdCloseListen 
         Caption         =   "停止侦听"
         Height          =   375
         Left            =   3960
         TabIndex        =   16
         Top             =   1560
         Width           =   1455
      End
      Begin VB.TextBox txtRecvLocalPort 
         Height          =   285
         Left            =   1680
         TabIndex        =   9
         Text            =   "5000"
         Top             =   360
         Width           =   1575
      End
      Begin VB.CommandButton cmdListen 
         Caption         =   "开始侦听"
         Height          =   375
         Left            =   3960
         TabIndex        =   8
         Top             =   720
         Width           =   1455
      End
      Begin VB.ListBox lstRecvData 
         Height          =   1425
         ItemData        =   "myUDP.frx":0000
         Left            =   120
         List            =   "myUDP.frx":0002
         TabIndex        =   6
         Top             =   720
         Width           =   3645
      End
      Begin MSWinsockLib.Winsock sockRecv 
         Left            =   5280
         Top             =   240
         _ExtentX        =   741
         _ExtentY        =   741
         _Version        =   393216
      End
      Begin VB.Label Label3 
         Caption         =   "接收端侦听端口"
         Height          =   255
         Left            =   165
         TabIndex        =   10
         Top             =   360
         Width           =   1335
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "发送端"
      Height          =   1935
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   5895
      Begin VB.CommandButton cmdCloseSend 
         Caption         =   "关闭socket"
         Height          =   375
         Left            =   3960
         TabIndex        =   15
         Top             =   840
         Width           =   1455
      End
      Begin VB.TextBox txtSendRemotePort 
         Height          =   285
         Left            =   1800
         TabIndex        =   13
         Text            =   "5000"
         Top             =   1080
         Width           =   1335
      End
      Begin VB.TextBox txtSendData 
         Height          =   285
         Left            =   1800
         TabIndex        =   11
         Top             =   1440
         Width           =   3615
      End
      Begin VB.CommandButton cmdSendDgram 
         Caption         =   "发送信息"
         Height          =   375
         Left            =   3960
         TabIndex        =   7
         Top             =   360
         Width           =   1455
      End
      Begin VB.TextBox txtSendLocalPort 
         Height          =   285
         Left            =   1800
         TabIndex        =   3
         Text            =   "1234"
         Top             =   720
         Width           =   1335
      End
      Begin VB.TextBox txtRecipientIP 
         Height          =   285
         Left            =   1800
         TabIndex        =   1
         Text            =   "10.11.111.129"
         Top             =   360
         Width           =   1695
      End
      Begin MSWinsockLib.Winsock sockSend 
         Left            =   120
         Top             =   960
         _ExtentX        =   741
         _ExtentY        =   741
         _Version        =   393216
      End
      Begin VB.Label Label5 
         Alignment       =   1  'Right Justify
         Caption         =   "接收端端口"
         Height          =   255
         Left            =   720
         TabIndex        =   14
         Top             =   1080
         Width           =   975
      End
      Begin VB.Label Label4 
         Alignment       =   1  'Right Justify
         Caption         =   "发送的信息"
         Height          =   255
         Left            =   720
         TabIndex        =   12
         Top             =   1440
         Width           =   975
      End
      Begin VB.Label Label1 
         Alignment       =   1  'Right Justify
         Caption         =   "本地绑定端口"
         Height          =   255
         Left            =   480
         TabIndex        =   4
         Top             =   720
         Width           =   1215
      End
      Begin VB.Label Label2 
         Alignment       =   1  'Right Justify
         Caption         =   "接收端的地址"
         Height          =   255
         Left            =   120
         TabIndex        =   2
         Top             =   360
         Width           =   1575
      End
   End
End
Attribute VB_Name = "udp"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub cmdSendDgram_Click()
    ' 绑定本地端口,然后发送数据
    If (sockSend.State = sckClosed) Then
        sockSend.RemoteHost = txtRecipientIP.Text
        sockSend.RemotePort = CInt(txtSendRemotePort.Text)
        sockSend.Bind CInt(txtSendLocalPort.Text)
        
        cmdCloseSend.Enabled = True
    End If
    '
    ' 现在发送数据
    '
    sockSend.SendData txtSendData.Text
End Sub
'"开始侦听"按钮单击事件
Private Sub cmdListen_Click()
    ' 绑定到本地端口
    '
    sockRecv.Bind CInt(txtRecvLocalPort.Text)
    '
    ' 改变控件的enable属性,因为单击侦听按钮两次会出现错误
    '
    cmdListen.Enabled = False
    cmdCloseListen.Enabled = True
End Sub

Private Sub cmdCloseSend_Click()
    ' 关闭发送socket,然后disable发送信息按钮
    '
    sockSend.Close
    cmdCloseSend.Enabled = False
End Sub

Private Sub cmdCloseListen_Click()
    ' 关闭服务socket
    '
    sockRecv.Close
    ' Enable侦听按钮
    '
    cmdListen.Enabled = True
    cmdCloseListen.Enabled = False
    lstRecvData.Clear
End Sub

Private Sub Form_Load()
    ' 初始化socket
    '
    sockSend.Protocol = sckUDPProtocol
    sockRecv.Protocol = sckUDPProtocol
    
    lblHostName.Caption = sockSend.LocalHostName
    lblLocalIP.Caption = sockSend.LocalIP
    
    cmdCloseListen.Enabled = False
    cmdCloseSend.Enabled = False
    
'    Timer1.Interval = 500
'    Timer1.Enabled = True
End Sub

Private Sub sockSend_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)
    MsgBox Description
End Sub

Private Sub sockRecv_DataArrival(ByVal bytesTotal As Long)
    Dim data As String
    
    ' 创建一个足够大的缓冲区来接受数据
    data = String(bytesTotal + 2, Chr$(0))
    sockRecv.GetData data, , bytesTotal
    lstRecvData.AddItem data
    ' 修改远程服务器属性
    '
    lblRemoteIP.Caption = sockRecv.RemoteHostIP
    lblRemotePort.Caption = sockRecv.RemotePort
End Sub

Private Sub sockRecv_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)
    MsgBox Description
End Sub

'Private Sub Timer1_Timer()
'    ' 修改socket的状态
'
'    Select Case sockSend.State
'        Case sckClosed
'            lblSenderState.Caption = "sckClosed"
'        Case sckOpen
'            lblSenderState.Caption = "sckOpen"
'        Case sckListening
'            lblSenderState.Caption = "sckListening"
'        Case sckConnectionPending
'            lblSenderState.Caption = "sckConnectionPending"
'        Case sckResolvingHost
'            lblSenderState.Caption = "sckResolvingHost"
'        Case sckHostResolved
'            lblSenderState.Caption = "sckHostResolved"
'        Case sckConnecting
'            lblSenderState.Caption = "sckConnecting"
'        Case sckClosing
'            lblSenderState.Caption = "sckClosing"
'        Case sckError
'            lblSenderState.Caption = "sckError"
'        Case Else
'            lblSenderState.Caption = "unknown"
'    End Select
'    Select Case sockRecv.State
'        Case sckClosed
'            lblReceiverState.Caption = "sckClosed"
'        Case sckOpen
'            lblReceiverState.Caption = "sckOpen"
'        Case sckListening
'            lblReceiverState.Caption = "sckListening"
'        Case sckConnectionPending
'            lblReceiverState.Caption = "sckConnectionPending"
'        Case sckResolvingHost
'            lblReceiverState.Caption = "sckResolvingHost"
'        Case sckHostResolved
'            lblReceiverState.Caption = "sckHostResolved"
'        Case sckConnecting
'            lblReceiverState.Caption = "sckConnecting"
'        Case sckClosing
'            lblReceiverState.Caption = "sckClosing"
'        Case sckError
'            lblReceiverState.Caption = "sckError"
'        Case Else
'            lblReceiverState.Caption = "unknown"
'    End Select
'End Sub

⌨️ 快捷键说明

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