客户端使用Binary Access Read方式打开文件,将内容读到一个二进制数组中,然后将这个二进制数组传送到服务器端,使用Binary Access Write方式写成文件即可。(客户端与服务器端可以互换,不受限制)下面是一个二进制读写文件的例子Dim getValue() As BytePrivate Sub Command1_Click() Open "C:\1.com" For Binary Access Write As #2 Put #2, , getValue() Close #2End SubPrivate Sub Form_Load()Open "C:\command.com" For Binary Access Read As #1 ReDim getValue(FileLen("C:\command.com")) Get #1, , getValue Close #1 End Sub
请教二进制数组传送到服务器端的方法,是否用SendData,好像有丢失字节的现象
Option ExplicitDim getValue() As Byte Dim LF As LongPrivate Sub Command1_Click() Open "C:\test1.bmp" For Binary Access Write As #2 Put #2, , getValue Close #2End SubPrivate Sub Form_Load() Open "C:\test.bmp" For Binary Access Read As #1 LF = LOF(1) ReDim getValue(1 To LF) Get #1, , getValue Close #1 End Sub
我以前页做过,是手工写的一个文件(0000 1111 2222 ... ffff)然后传送,我发现在发送的时候只要winsock1.senddata filearray()就可以了,但是在接受的那一方的winsock1_dataarrival的事件中一次最多只能收8K,你可以debug一下就可以看到了。所以我的方法是在收的时候加一个计数,直到将整个文件收完再close。你看一下吧。>>> send Private Sub Command1_Click() Winsock1.RemoteHost = "127.0.0.1" Winsock1.RemotePort = 9988 Winsock1.Connect End SubPrivate Sub Command2_Click() Dim FileArray() As Byte Open "E:\OICQ\qq.exe" For Binary As #1 '我们测试一个>8K的文件 'ReDim FileArray(lof(1)) 'at first you should tell the client the correct file length ReDim FileArray(1720390) 'at first you should tell the client the correct file length Get #1, , FileArray Winsock1.SendData FileArray Close #1 End Sub >>> ReceiveDim FileCount As LongPrivate Sub Form_Load() Winsock1.LocalPort = 9988 Winsock1.Listen End SubPrivate Sub Winsock1_Close() Winsock1.Close Label1.Caption = "Connect Close" End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long) Winsock1.Close Winsock1.Accept requestID Label1.Caption = "Connect OK"'Open File for write Open "e:\oicq\qq2.exe" For Binary As #1 End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim getArray() As Byte ReDim getArray(bytesTotal) Winsock1.GetData getArrayFileCount = FileCount + bytesTotal Label1.Caption = CStr(Int(FileCount * 100 / 1720390)) + " %" '1720390 is the file length. At first you should try to get the correct file length If FileCount <= 1720390 + 1 Then '1720390 is the file length. At first you should try to get the correct file length '1720390+1 是为了文件的实际长度和数组的序号匹配 Put #1, , getArray Else Close #1 Label1.Caption = "Receive OK!" End If End Sub
Open "C:\1.com" For Binary Access Write As #2
Put #2, , getValue()
Close #2End SubPrivate Sub Form_Load()Open "C:\command.com" For Binary Access Read As #1
ReDim getValue(FileLen("C:\command.com"))
Get #1, , getValue
Close #1
End Sub
Dim LF As LongPrivate Sub Command1_Click()
Open "C:\test1.bmp" For Binary Access Write As #2
Put #2, , getValue
Close #2End SubPrivate Sub Form_Load() Open "C:\test.bmp" For Binary Access Read As #1
LF = LOF(1)
ReDim getValue(1 To LF)
Get #1, , getValue
Close #1
End Sub
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = 9988
Winsock1.Connect
End SubPrivate Sub Command2_Click()
Dim FileArray() As Byte
Open "E:\OICQ\qq.exe" For Binary As #1 '我们测试一个>8K的文件
'ReDim FileArray(lof(1)) 'at first you should tell the client the correct file length
ReDim FileArray(1720390) 'at first you should tell the client the correct file length
Get #1, , FileArray
Winsock1.SendData FileArray
Close #1
End Sub
>>> ReceiveDim FileCount As LongPrivate Sub Form_Load()
Winsock1.LocalPort = 9988
Winsock1.Listen
End SubPrivate Sub Winsock1_Close()
Winsock1.Close
Label1.Caption = "Connect Close"
End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
Label1.Caption = "Connect OK"'Open File for write
Open "e:\oicq\qq2.exe" For Binary As #1
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim getArray() As Byte
ReDim getArray(bytesTotal)
Winsock1.GetData getArrayFileCount = FileCount + bytesTotal
Label1.Caption = CStr(Int(FileCount * 100 / 1720390)) + " %" '1720390 is the file length. At first you should try to get the correct file length
If FileCount <= 1720390 + 1 Then
'1720390 is the file length. At first you should try to get the correct file length
'1720390+1 是为了文件的实际长度和数组的序号匹配
Put #1, , getArray
Else
Close #1
Label1.Caption = "Receive OK!"
End If
End Sub