此代码是csdn中一位网友给的,但我使用后发现虽然可以传输,但接收到的文件大小不超过1kb,也好像是说服务器接到数据后写文件时没有接着写,其中变量Is_FILESEND 好像有问题,,   请高手指点!!

解决方案 »

  1.   

    Server:Private Sub Form_Load()
    sckFser.Listen
    End SubPrivate Sub sckFser_ConnectionRequest(ByVal requestID As Long)' Text1 = requestID
     
     'On Error GoTo IDERROR
    If sckFser.State <> sckClosed Then sckFser.Close
     sckFser.Accept requestID'IDERROR:
         'MsgBox Err.Description, vbCritical
    End Sub
    Private Sub sckFser_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
     Dim sData As String
     Dim lRet As Long
     Dim DataByte() As Byte
     Dim SendFileLen
     
     sckFser.GetData DataByte
     
     strData = StrConv(DataByte, vbUnicode)
       MsgBox ""
       
         
    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
        Text1.Text = strData
        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
    End Sub
      

  2.   

    'Client:Private Sub Command1_Click()
    sckFcli.SendData "Sdfsf"End SubPrivate Sub Command2_Click()
    Dim Fp As String
    Fp = Text1.TextSendFile Fp, "c:\", sckFcli, ProBarEnd SubPrivate Sub Form_Load()
    If sckFcli.State <> 0 Then Winsock1.Close
    sckFcli.Connect
    'sckFcli.SendData "dd"End Sub
    '''''''''''''''''''''模块''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''Declare Function GetTickCount Lib "kernel32" () As Long
    Const SendDataSize = 1024''''Pause''''''''''''''Sub Sleep(HowLong As Long)
        Dim u%, tick As Long
        tick = GetTickCount()
        
        Do
          u% = DoEvents
        Loop Until tick + HowLong < GetTickCount
    End Sub
    '''''''''''''
    Public Sub SendFile(FileName As String, RemoteFilePath As String, Wins As Winsock, objProBar As Xp_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
    'Wins.RemoteHost = "127.0.0.1"
    'Wins.RemotePort = "33"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 "Sdfsf"
     
        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
      

  3.   

    确实非常乱的代码。
    Is_FILESEND是进行接收文件的标志,标志段竟然没用split来取得不同段的内容
    而是自己拆解,复杂且容易错。服务端要错就是这段拆分取得文件长度时有错误,
    其他应该没有。不过服务端这种处理方式并不怎么好。
    客户端的循环方式senddata不知道可不可以,代码例子都是sendcomplete后才进行
    下次senddata的。