有谁可以给我一个用Winsock点对点传输文件的例子?本人及用啊!!谢谢了~~
我自己做了一个,文本文件没有问题,可是我传BMP的时候就发生两个文件不一致的情况,看来是我的方法问题,大家有没有好一点的方法,各给我参考参考。谢了! 问题点数:20、回复次数:2Top
1 楼Gelim(Gelim)回复于 2003-11-04 01:20:21 得分 10
Public Sub SendFile(FileName As String, RemoteFilePath As String, WinS As Winsock, objProBar As ProgressBar)
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 String
FreeF = 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)
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
Top
2 楼j4sxw(HELLOworld :))回复于 2003-11-04 10:05:15 得分 10
'我自己的发送端函数:
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 String
FreeF = 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
Top




