我给你一个源代码!希望能对你有用!'我自己的发送端函数:Public Sub SendFile(FileName As String, RemoteFilePath As String, WinS As Winsock, objProBar As ProgressBar)'FileName 是个本地需要发送的文件名(包含全路经) 'RemoteFilePath 是个远端接收文件的地址(包含全路经,不包括文件名) 'WinS是个Winsock对象,objProBar是个进度条对象 'const SendDataSize =1024 Dim FreeF As Integer Dim LenFile As Long Dim nCnt As Long Dim LocData() As Byte Dim Tempstr As String Dim a() As Byte Dim i As Long Dim myHead As StringFreeF = FreeFile Open FileName For Binary As FreeF nCnt = 1 LenFile = FileLen(FileName)
Do Until nCnt > (LenFile) DoEvents If nCnt + SendDataSize - 1 > LenFile Then ReDim LocData(LenFile - nCnt) As Byte Else ReDim LocData(SendDataSize - 1) As Byte End If Get FreeF, nCnt, LocData 'Get data from the file nCnt is from where to start the get
WinS.SendData LocData
nCnt = nCnt + SendDataSize
objProBar.Value = objProBar.Value + 1 Loop Close FreeF objProBar.Value = objProBar.Max objProBar.Visible = False End Sub'我自己的接收端程序:Private Sub objTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strData As String Dim sData As String Dim lRet As Long Dim DataByte() As Byte
objTCP(intmax).GetData DataByte
strData = StrConv(DataByte, vbUnicode)
If Is_FILESEND = True Then 'Is_FILESEND是个全局变量 Put #myFreeFile, , DataByte SendFileLen = SendFileLen - UBound(DataByte) - 1
If SendFileLen <= 0 Then Close #myFreeFile myFreeFile = 0 Is_FILESEND = False End If Else If InStr(1, strData, "|FILESEND|") <> 0 Then Dim sFileName As String Dim k As Integer Is_FILESEND = True k = InStr(11, strData, "|") sFileName = Mid$(strData, 11, k - 11) SendFileLen = CLng(right$(strData, Len(strData) - k)) myFreeFile = FreeFile Open sFileName For Binary As myFreeFile End If ......... '其他程序End If
呵呵~~
思路是这样得。
'RemoteFilePath 是个远端接收文件的地址(包含全路经,不包括文件名)
'WinS是个Winsock对象,objProBar是个进度条对象
'const SendDataSize =1024
Dim FreeF As Integer
Dim LenFile As Long
Dim nCnt As Long
Dim LocData() As Byte
Dim Tempstr As String
Dim a() As Byte
Dim i As Long
Dim myHead As StringFreeF = FreeFile
Open FileName For Binary As FreeF nCnt = 1
LenFile = FileLen(FileName)
Tempstr = IIf(Right$(RemoteFilePath, 1) = "\", RemoteFilePath & _
Right$(FileName, Len(FileName) - InStrRev(FileName, "\")), RemoteFilePath & _
"\" & Right$(FileName, Len(FileName) - InStrRev(FileName, "\")))
myHead = "|FILESEND|" & Tempstr & "|" & CStr(LenFile)
WinS.SendData myHead '发送头和文件名及文件总长度!
objProBar.Value = 0
objProBar.Max = Fix(LenFile / SendDataSize) + 1
objProBar.Visible = True
Sleep (300) '一个api函数
Do Until nCnt > (LenFile)
DoEvents
If nCnt + SendDataSize - 1 > LenFile Then
ReDim LocData(LenFile - nCnt) As Byte
Else
ReDim LocData(SendDataSize - 1) As Byte
End If
Get FreeF, nCnt, LocData 'Get data from the file nCnt is from where to start the get
WinS.SendData LocData
nCnt = nCnt + SendDataSize
objProBar.Value = objProBar.Value + 1
Loop
Close FreeF
objProBar.Value = objProBar.Max
objProBar.Visible = False
End Sub'我自己的接收端程序:Private Sub objTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strData As String
Dim sData As String
Dim lRet As Long
Dim DataByte() As Byte
objTCP(intmax).GetData DataByte
strData = StrConv(DataByte, vbUnicode)
If Is_FILESEND = True Then 'Is_FILESEND是个全局变量
Put #myFreeFile, , DataByte
SendFileLen = SendFileLen - UBound(DataByte) - 1
If SendFileLen <= 0 Then
Close #myFreeFile
myFreeFile = 0
Is_FILESEND = False
End If
Else
If InStr(1, strData, "|FILESEND|") <> 0 Then
Dim sFileName As String
Dim k As Integer
Is_FILESEND = True
k = InStr(11, strData, "|")
sFileName = Mid$(strData, 11, k - 11)
SendFileLen = CLng(right$(strData, Len(strData) - k))
myFreeFile = FreeFile
Open sFileName For Binary As myFreeFile
End If ......... '其他程序End If