我给你一个源代码!希望能对你有用!'我自己的发送端函数: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
To Gelim(Gelim) : 呵呵,在这里也看到你啊。
好。凑个热闹,我把我才写好的发送文件的代码写下, 发送: Dim myfile() As Byte Dim position As Long Dim FileName As String
FileName = txtaddress.Text position = FileLen(FileName) ReDim myfile(position - 1) As Byte
Open FileName For Binary As #1 Get #1, , myfile Close #1
sockclient.SendData myfile接收端:Private lenth As Long '已经收到的数据的长度,再添加时以这个长度为依据Private Sub sockserver_DataArrival(ByVal bytesTotal As Long) Dim receivefile() As Byte, i As Long ReDim receivefile(1 To bytesTotal) '次处也可以是(0 To bytesTotal-1) sockserver.GetData receivefile, vbArray + vbByte Open "c:\a.bmp" For Binary As #1 Put #1, lenth + 1, receivefile Close #1
http://www.dapha.net/down/list.asp?id=1003
http://www.dapha.net/down/list.asp?id=1003
'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
呵呵,在这里也看到你啊。
发送:
Dim myfile() As Byte
Dim position As Long
Dim FileName As String
FileName = txtaddress.Text
position = FileLen(FileName)
ReDim myfile(position - 1) As Byte
Open FileName For Binary As #1
Get #1, , myfile
Close #1
sockclient.SendData myfile接收端:Private lenth As Long '已经收到的数据的长度,再添加时以这个长度为依据Private Sub sockserver_DataArrival(ByVal bytesTotal As Long)
Dim receivefile() As Byte, i As Long
ReDim receivefile(1 To bytesTotal) '次处也可以是(0 To bytesTotal-1) sockserver.GetData receivefile, vbArray + vbByte Open "c:\a.bmp" For Binary As #1
Put #1, lenth + 1, receivefile
Close #1
lenth = lenth + UBound(receivefile) - LBound(receivefile) + 1
txtmessage.Text = txtmessage.Text & Time & "收到数据" & Chr(13) & Chr(10)
End Sub