用Winsock,TCP协议传送,确认连接成功后,可用下面的函数进行传送与接收
'这两个函数实现了winsock控件传送和接收文件的功能,最大能传送 524288G文件,但传超大文件时实际效率比较_低,传送文件基本思路是以二制模式分块传递,系统资源占用高,可以稍微修改实现多线程传送
Const Max As Long = 65534 '每次最大传送数据
Dim SendPos As Double '发送数据位置
Dim RecPos As Double '接收数据位置(此二变量可实现断点续传)
Sub SendFile(FileName As String, Wnk As Winsock)
Static iPoss As Double '当前发送位置
Dim SendData() As Byte '二进制数据
Dim Length As Double '记录文件长度
Dim FileNum As Integer
FileNum = FreeFile '获得文件号
Length = FileLen(FileName) '获得文件长度
Open FileName For Binary As FileNum
DoEvents
If Length <= Max Then
ReDim SendData(1 To Length)
Get FileNum, , SendData
Wnk.SendData SendData
Else
While iPos <= Length - Max
ReDim SendData(1 To Max)
Get FileNum, iPos + 1, SendData
Wnk.SendData SendData
iPos = iPos + Max
Wend
End If
ReDim SendData(Length - iPos - 1) '此处注意要-1,否则不会成功!
Get FileNum, iPos + 1, SendData
Wnk.SendData SendData
Close FileNum
Debug.Print FileLen(FileName)
End Sub
'==============================================================
'==============================================================
Sub ReceiveData(FileName As String, Wnk As Winsock, Lens As Long) '接收数据
Dim RecData() As Byte
Dim Length As Double
Dim FileNum As Integer
FileNum = FreeFile
Open FileName For Binary As FileNum
Length = FileLen(FileName)
ReDim RecData(1 To Lens)
Wnk.GetData RecData
Put FileNum, Length + 1, RecData
Close FileNum
End Sub