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

📄 frmsender.frm

📁 vb发送文件,检查邮件,串口操作,查看文件等关于socket的源代码!
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form FrmSender 
   Caption         =   "发送(WinSock)"
   ClientHeight    =   2895
   ClientLeft      =   2055
   ClientTop       =   4860
   ClientWidth     =   5970
   LinkTopic       =   "Form1"
   ScaleHeight     =   2895
   ScaleWidth      =   5970
   Begin VB.CommandButton cmdSendText 
      Caption         =   "&Ok"
      Height          =   285
      Left            =   4875
      TabIndex        =   7
      Top             =   720
      Width           =   540
   End
   Begin VB.TextBox txtTalk 
      Height          =   690
      Left            =   1200
      TabIndex        =   5
      Top             =   720
      Width           =   3615
   End
   Begin MSWinsockLib.Winsock udpTalk 
      Left            =   1575
      Top             =   225
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin MSComctlLib.StatusBar StatusBar1 
      Align           =   2  'Align Bottom
      Height          =   330
      Left            =   0
      TabIndex        =   4
      Top             =   2565
      Width           =   5970
      _ExtentX        =   10530
      _ExtentY        =   582
      _Version        =   393216
      BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
         NumPanels       =   1
         BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            AutoSize        =   1
            Object.Width           =   10107
         EndProperty
      EndProperty
   End
   Begin VB.CommandButton cmdSend 
      Caption         =   "&H 开始发送"
      Height          =   375
      Left            =   1200
      TabIndex        =   3
      Top             =   2025
      Width           =   1365
   End
   Begin MSComDlg.CommonDialog dlg 
      Left            =   2850
      Top             =   180
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.CommandButton cmdPickFile 
      Caption         =   "1"
      BeginProperty Font 
         Name            =   "Wingdings"
         Size            =   9
         Charset         =   2
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   4725
      TabIndex        =   2
      Top             =   1530
      Width           =   540
   End
   Begin VB.TextBox txtFileName 
      Height          =   285
      Left            =   1200
      TabIndex        =   1
      Top             =   1575
      Width           =   3465
   End
   Begin MSWinsockLib.Winsock tcpData 
      Left            =   1050
      Top             =   225
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "会话"
      Height          =   180
      Left            =   750
      TabIndex        =   6
      Top             =   765
      Width           =   360
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "待发送文件"
      Height          =   180
      Left            =   225
      TabIndex        =   0
      Top             =   1620
      Width           =   900
   End
End
Attribute VB_Name = "FrmSender"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdPickFile_Click()
    With dlg
        .DialogTitle = "请挑选一个待发送的文件"
        .Filter = "*.*"
        .ShowOpen
        If Len(Trim(.FileName)) > 0 Then
            Me.txtFileName = .FileName
        End If
    End With
End Sub

Private Sub cmdSend_Click()
    If Me.txtFileName = "" Or Len(Dir(Me.txtFileName)) = 0 Then
        MsgBox "文件没有挑选,或者不存在"
        Exit Sub
    End If
    
    SetTipText "准备与远端主机通讯 ..."
    With Me.udpTalk
        .RemoteHost = "127.0.0.1"
        .SendData ":FileName=" & Mid(Me.txtFileName, InStrRev(Me.txtFileName, "\") + 1)
    End With
    
    Call SendFile(Me.txtFileName)
    
End Sub

Private Sub SendFile(ByVal strFileName As String)
    ' 建立TCP连接
    With Me.tcpData
        .Protocol = sckTCPProtocol
        .RemoteHost = "127.0.0.1"
        .RemotePort = "1234"
        .Connect
        
        Call Pause(180)         ' 延时
        
        If .State <> sckConnected Then
            SetTipText "超时,无法连接 "
            Exit Sub
        End If
        
        ' 开始传输文件
        Dim hFile As Long
        Dim L As Long, byt() As Byte
        
        hFile = FreeFile
        Open (Me.txtFileName) For Binary Access Read As #hFile
        
        ReDim byt(0 To 1023)
        
        Do While Not EOF(hFile)
            Get hFile, , byt
            .SendData byt
            SendTalkText StrConv(byt, vbUnicode)
            Call Pause(3)         ' 延时
        Loop
        
        Close hFile
        
        SendTalkText "文件发送完成"
    
    End With
End Sub

Private Sub SendTalkText(ByVal TalkText As String)
    With Me.udpTalk
        .SendData TalkText
    End With
End Sub
Private Sub SetTipText(ByVal TipText As String)
    Me.StatusBar1.Panels(1).Text = TipText
End Sub

Private Sub cmdSendText_Click()
    Call SendTalkText(Me.txtTalk)
End Sub

Private Sub Form_Load()
    With Me.udpTalk
        .Protocol = sckUDPProtocol
        .RemoteHost = "127.0.0.1"
        .RemotePort = 9999
        .LocalPort = 9990
    End With
    With Me.tcpData
        .Protocol = sckTCPProtocol
        .RemoteHost = "127.0.0.1"
        .RemotePort = "1234"
    End With
End Sub

⌨️ 快捷键说明

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