当前位置:编程学习 > VB >>

用winsock传输文件后,只能打开txt文件,doc文件无法打开,高手帮帮忙,谢谢!

发送端:
Public Sub SendFile()
    On Error GoTo 1
    pbTotal.Value = 0
    pbUploaded.Value = 0
    pbUploaded.Max = FileSize
    pbTotal.Max = FileSize
    Caption = "正在读取与上载中..."
    
    Dim intFile As Integer
    Dim strData As String
    Dim strInput As String
    Dim dblSent As Double
    intFile = FreeFile
    
    Open opendialog.FileName For Binary As #intFile
    '检查是否小文件
    If FileSize <= lngInputSize Then
        strInput = Space(FileSize)
        Get #intFile, , strInput
        wsTransfer.SendData strInput
        GoTo 1
    End If
    '超过 1KB - 将分段为 1024 字节
    
    Do While EOF(intFile) = False
        If dblSent + lngInputSize <= FileSize Then
            strInput = Space(lngInputSize)
            Get #intFile, , strInput
            wsTransfer.SendData strInput
            dblSent = dblSent + lngInputSize
            DoEvents
        Else
            strInput = Space(FileSize - dblSent)
            Get #intFile, , strInput
            wsTransfer.SendData strInput
            Exit Do
        End If
        DoEvents
    Loop
1:
    pbTotal.Value = FileSize
    wsInfo.SendData "COMPLETE|COMPLETE" & vbCrLf
    DoEvents
    MsgBox opendialog.FileName & " 文件发送成功!", vbInformation, "文件传输"
    DoEvents
    Close #intFile
    Unload Me
End Sub


接收端:
Private Sub wsInfo_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    On Error Resume Next
    
    Dim strInput As String
    Dim strData() As String
    Dim strParse() As String
    Dim strItem As String
    Dim strText As String
    Dim item As ListItem
    Dim i As Integer
    
    wsInfo(Index).GetData strInput, , bytesTotal
    strData = Split(strInput, vbCrLf)
    For i = 0 To UBound(strData)
        strText = ""
        strText = strData(i)
        If strText <> "" Then
        strParse = Split(strText, "|")
        strText = strParse(0)
        strItem = strParse(1)
        Set item = lvFiles.ListItems(Index + 1)
        Select Case UCase(strText)
                Case "FILENAME"
                    item.SubItems(1) = strItem
                    Kill App.Path & "\Downloads\" & lvFiles.ListItems(Index + 1).SubItems(1)
                Case "FILEDATE"
                    item.Tag = strItem
                    Dim frmDL As New frmDownload
                    frmDL.lblFile.Caption = item.SubItems(1)
                    frmDL.lblIP.Caption = item.SubItems(3)
                    frmDL.intSocket = Index
                    frmDL.Show , Me
                Case "FILESIZE"
                    FileSize(Index) = strItem
                    item.SubItems(2) = "0/" & strItem
                Case "SENDING"
                    item.Text = "下载中"
                Case "COMPLETE"
                    item.Text = "完成"
                Case "SEND"
                    item.Text = "下载中"
        End Select
        
        End If
    Next i
End Sub
--------------------编程问答-------------------- --------------------编程问答-------------------- 友情up --------------------编程问答-------------------- dim buf as byte()
redim buf(FileSize-1)
...
buf=inputb(FileSize,#f)
...
wsTransfer.SendData buf

dim FileSize as long
dim TotalSize as long
dim buf as byte()
dim buf_this_time as byte()
dim i as long
redim buf_this_time(bytesTotal-1)
wsInfo(Index).GetData buf_this_time, , bytesTotal
redim buf(TotalSize+bytesTotal)
for i=0 to bytesTotal
    buf(TotalSize+i)=buf_this_time(i)
next
TotalSize=TotalSize+bytesTotal
if TotalSize>=FileSize then 全部接收完毕 --------------------编程问答-------------------- 分块的时候打个包,把里面数据的CRC与长度都包含在包头里

接收的时候做个效验,不正确就通知发送方重发

自己得定个协议才行. --------------------编程问答-------------------- 我的也是,高手出山啊 --------------------编程问答-------------------- 谢谢大家!!!!不过还没调试成功  --------------------编程问答--------------------
引用 6 楼 tj131016 的回复:
谢谢大家!!!!不过还没调试成功

能成功才是怪事!
.doc 文档居然用 字符串 方式来读取,楼主太强了…………



你应该用 Byte数组进行读取啊!!!
--------------------编程问答-------------------- http://www.m5home.com/blog/article.asp?id=169

要有个协议,然后所有传输的文件都以字节方式收发,如楼上所说. --------------------编程问答-------------------- 学习……
补充:VB ,  网络编程
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,