使用 二进制数组传递文件,winsock 可以完成

解决方案 »

  1.   


    定义一个byte类型的数组
    把你要传的文件读出来,放到这个数组中
    然后又winsock传送就可以了
      

  2.   

    在服务器端监听,服务端为:其中wsk为winsock控件
    Option Explicit
     Dim flag As Boolean, complete As Boolean
     Dim sendsize As Long, length As Long
     Dim size As Long
     Dim filenum As Long
    Private Sub Command1_Click()   '传送文件按钮
           flag = True
           sdata
    End SubPrivate Sub Form_Load()
    With Wsk
       .Protocol = sckTCPProtocol
       .LocalPort = 5051
       .Listen
    End With
    size = 5734
    End SubPrivate Sub Wsk_ConnectionRequest(ByVal requestID As Long)
       Wsk.Close
       Wsk.Accept requestID
       Wsk.SendData "ok|" + CStr(FileLen("C:\Documents and Settings\Administrator\桌面\18225.gif"))
    End SubPrivate Sub Wsk_DataArrival(ByVal bytesTotal As Long)
       Dim s As String
       Wsk.GetData s
       Select Case s
          Case "get"
              sdata
       End Select
    End SubSub sdata()
     Dim chuck() As Byte      If flag Then
            If filenum = 0 Then
               filenum = FreeFile
               Open "C:\Documents and Settings\Administrator\桌面\18225.gif" For Binary As #filenum
            End If
               length = FileLen("C:\Documents and Settings\Administrator\桌面\18225.gif")
               If (LOF(filenum) - Loc(filenum)) < size Then
                 size = (LOF(filenum) - Loc(filenum))
               End If
               ReDim chuck(0 To size - 1)
               Get #filenum, , chuck
              
               Wsk.SendData chuck
                sendsize = sendsize + size          If sendsize = length Then
                  complete = True
                  Close #filenum
               End If
          End If
    End SubPrivate Sub Wsk_SendComplete()
          DoEvents
          If filenum > 0 Then
           If Not complete Then
                sdata
            Else
                Close #filenum
                Wsk.Close
                Debug.Print "complete"
            End If
          End If
    End Sub客户端:   Option Explicit
    Dim length As Long
    Dim Recbyte As Long
    Dim i As Integer
    Dim flag As Boolean
    Dim fnum As IntegerPrivate Sub connect_Click()   '连接按钮
       wsk.connect
    End SubPrivate Sub Form_Load()
       With wsk
          .RemoteHost = "127.0.0.1"
          .RemotePort = 5051
          .Protocol = sckTCPProtocol
       End With
    End SubPrivate Sub receive_Click()
       wsk.SendData "rec"
    End SubPrivate Sub wsk_DataArrival(ByVal bytesTotal As Long)
      Dim str As String
      Dim tt() As String
      If i = 0 Then
         wsk.GetData str
         Text1.Text = str
         tt = Split(str, "|")
         If tt(0) = "ok" Then
           wsk.SendData "get"
           length = CLng(tt(1))
           receive.Enabled = True
           
           flag = True     End If
         i = i + 1
      Else
          getbyte bytesTotal
      End If
        Lab.Caption = Err.Description
    End SubPrivate Sub wsk_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
          Lab.Caption = "error when connect"
          wsk.Close
    End Sub
    Sub getbyte(bytesTotal As Long)
         Dim byt() As Byte
         ReDim byt(0 To bytesTotal - 1)
         Recbyte = Recbyte + bytesTotal       wsk.GetData byt, vbArray + vbByte
           Text1.Text = byt
           If fnum = 0 Then
           fnum = FreeFile
           Open "d:\025.jpg" For Binary As #fnum
           End If
           Put #fnum, , byt
           If length = Recbyte Then
              wsk.SendData "end"
              Close #fnum
              Debug.Print "get data complete"
              Exit Sub
           End If
    End Sub