'新建两个窗体form1和form2
'============================发送端============================
'引用以下几个控件
'Microsoft Windows common Controls 6.0 进度条控件
'Microsoft Winsock Control 6.0 Winsock控件
'一个按钮,三个文件框
Option Explicit
Private Sub Command1_Click()
Dim BytDate() As Byte '文件数组
Dim FileName As String '路径
Dim lngFile As Long
Dim i As Long
FileName = "c:\ok.exe " '取得文件名及路径
lngFile = FileLen(FileName) \ 1024 '取得文件长度
ProgressBar1.Min = 0
ProgressBar1.Max = lngFile + 1
ProgressBar1.Value = 0
For i = 0 To lngFile
ReDim myFile(1023) As Byte '初始化数组
Open FileName For Binary As #1 '打开文件
Get #1, i * 1024 + 1, myFile '将文件写入数组
Close #1 '关闭文件
Winsock1.SendData myFile '发送
DoEvents
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = ProgressBar1.Max Then MsgBox "OK" '判断是否传完
Dim abc As Long
abc = lngFile / 100 '将文件大小分成100份
Text1 = ProgressBar1.Value '已发送多少
Text2 = lngFile '总大小
Text3 = ProgressBar1.Value / abc '百分比
Next i
End Sub
Private Sub Form_Load()
Form2.Show '显示form2
Winsock1.Protocol = sckTCPProtocol '设为tcp协议
Winsock1.LocalPort = 2001 '本机端口
Winsock1.RemotePort = 10 '远程端口
Winsock1.RemoteHost = "192.168.27.7"
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> 0 Then
Winsock1.Close
Winsock1.Accept requestID
End If
End Sub
'============================接收端============================
'引用以下几个控件
'Microsoft Winsock Control 6.0 Winsock控件
Option Explicit
Private Sub Form_Load()
Winsock1.Protocol = sckTCPProtocol '设为tcp协议
Winsock1.RemoteHost = "192.168.27.7" '发送端IP
Winsock1.LocalPort = 2001 '本机端口
Winsock1.RemotePort = 10 '远程端口
Winsock1.Connect '连接发送端
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = "d:\yes.exe"
ReDim myFile(bytesTotal - 1) '此处也可以是(0 To bytesTotal-1)
Winsock1.GetData myFile
Open myPath For Binary As #1 '新建文件
myLong = FileLen(myPath)
Put #1, myLong + 1, myFile '将收到的数据写入新文件中
Close #1 '关闭
End Sub