我是这么做的: Private Sub tcpServer_DataArrival (ByVal bytesTotal As Long) Dim ArrivedArray() As Byte ReDim ArrivedArray(bytesTotal) As Byte Dim i As Integer
Dim strFilePath2 As String strFilePath2 = "D:\3.jpg"
tcpServer.GetData ArrivedArray, vbArray + vbByte, bytesTotal Data(UBound(Data)) = ArrivedArray If bytesTotal < 8192 Then '8192是每次发送的最大的数据块大小 Open strFilePath2 For Binary Access Write As #1 For i = LBound(Data) To UBound(Data) ArrivedArray = Data(i) MsgBox (UBound(ArrivedArray)) Put #1, , ArrivedArray Next Close #1 Exit Sub End If
在数据接收端我发现一个有趣的现象。使用GetData PicBit(1) bytesTotal是垒加。记第一次是8092 ,第二次是16184.......如果是GetData Buff ,bytesTotal是8092(文件大于8092).而且如果用函数检测数组UBound(Buff)=8091 .而不是8092。当选用第一种方法时 winsock控件负责填充整个数组但是最后会发现,数组比原始数组要小。所以只能使用第二种方法。要自己实现数组的拼接。就是上面的不同啊!就是说当给GetData函数传入不同形式的参数.得道的结果不同.一个市数组的第一个元素.在调用api函数时,相当于数组的指针.一个是数组名称.在c里可以作为数组的指针.在vb里我也有些犯迷糊.但结果肯定不同.这是我写的数据接收段的代码.效率不高. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Dim PicBits() As Byte Dim buff() As Byte Dim lAlreadyTran As Long Private Sub Command1_Click() ReDim PicBits(0) As Byte lAlreadyTran = 0 ws2.Close ws2.Listen End SubPrivate Sub Form_Load() ws2.Bind 10001, "127.0.0.1" ws2.Listen lAlreadyTran = 0 End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) ws2.Close Picture1 = Nothing End Sub Private Sub ws2_ConnectionRequest(ByVal requestID As Long) ws2.Close ws2.Accept requestID End SubPrivate Sub ws2_DataArrival(ByVal bytesTotal As Long) Dim i As Long Dim j As Long Dim dummy() As Byte ReDim buff(bytesTotal) As Byte ws2.GetData buff, vbbytes, bytesTotal ReDim dummy(lAlreadyTran) As Byte For j = 1 To lAlreadyTran dummy(j) = PicBits(j) Next ReDim PicBits(lAlreadyTran + bytesTotal) For j = 1 To lAlreadyTran PicBits(j) = dummy(j) Next ReDim dummy(0) As Byte For i = 1 To bytesTotal PicBits(lAlreadyTran + i) = buff(i - 1) NextlAlreadyTran = lAlreadyTran + bytesTotalSetBitmapBits Picture1.Image, lAlreadyTran, PicBits(1) Picture1.Refresh End Sub 多给点分吧! :)
Private Sub tcpServer_DataArrival (ByVal bytesTotal As Long) Dim ArrivedArray() As Byte
ReDim ArrivedArray(bytesTotal) As Byte
Dim i As Integer
Dim strFilePath2 As String
strFilePath2 = "D:\3.jpg"
tcpServer.GetData ArrivedArray, vbArray + vbByte, bytesTotal
Data(UBound(Data)) = ArrivedArray
If bytesTotal < 8192 Then '8192是每次发送的最大的数据块大小
Open strFilePath2 For Binary Access Write As #1
For i = LBound(Data) To UBound(Data)
ArrivedArray = Data(i)
MsgBox (UBound(ArrivedArray))
Put #1, , ArrivedArray
Next
Close #1
Exit Sub
End If
ReDim Preserve Data(UBound(Data) + 1)
End Sub
gameboy999和pp616朋友:谢谢你们,.cn
发送数据.只是数据接受端麻烦了点。
winsock控件负责填充整个数组但是最后会发现,数组比原始数组要小。所以只能使用第二种方法。要自己实现数组的拼接。
这个小程序困扰我一个星期了,头都搞大了,别再开玩笑了。jlum99:你说的两种方法有什么不一样,我好象还没看出来。 我正是用函数getbitmapbits得到位图数组PicBit()。直接使用winsock1.senddata PicBit发送数据.数据接受端怎么办,再指点指点。谢了。
winsock控件负责填充整个数组但是最后会发现,数组比原始数组要小。所以只能使用第二种方法。要自己实现数组的拼接。就是上面的不同啊!就是说当给GetData函数传入不同形式的参数.得道的结果不同.一个市数组的第一个元素.在调用api函数时,相当于数组的指针.一个是数组名称.在c里可以作为数组的指针.在vb里我也有些犯迷糊.但结果肯定不同.这是我写的数据接收段的代码.效率不高.
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim PicBits() As Byte
Dim buff() As Byte
Dim lAlreadyTran As Long
Private Sub Command1_Click()
ReDim PicBits(0) As Byte
lAlreadyTran = 0
ws2.Close
ws2.Listen
End SubPrivate Sub Form_Load()
ws2.Bind 10001, "127.0.0.1"
ws2.Listen
lAlreadyTran = 0
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
ws2.Close
Picture1 = Nothing
End Sub
Private Sub ws2_ConnectionRequest(ByVal requestID As Long)
ws2.Close
ws2.Accept requestID
End SubPrivate Sub ws2_DataArrival(ByVal bytesTotal As Long)
Dim i As Long
Dim j As Long
Dim dummy() As Byte
ReDim buff(bytesTotal) As Byte
ws2.GetData buff, vbbytes, bytesTotal
ReDim dummy(lAlreadyTran) As Byte
For j = 1 To lAlreadyTran
dummy(j) = PicBits(j)
Next
ReDim PicBits(lAlreadyTran + bytesTotal)
For j = 1 To lAlreadyTran
PicBits(j) = dummy(j)
Next
ReDim dummy(0) As Byte
For i = 1 To bytesTotal
PicBits(lAlreadyTran + i) = buff(i - 1)
NextlAlreadyTran = lAlreadyTran + bytesTotalSetBitmapBits Picture1.Image, lAlreadyTran, PicBits(1)
Picture1.Refresh
End Sub
多给点分吧! :)
由于这段代码编得时间太长了,无奈何,我只好还是用了传文件的方法,先抓图存为文件,再用WINSOCK传输到控制端。传文件的方法很顺利,现已经可监视屏幕了,当然我认为传数据流是最好的方法,等我按你的方法试试再向你请教。
我觉得不可思议的是:其实传文件与传数据是一个道理,只是一个是接收到一块后写到文件中,为什么它就不出错呢?
另你说的数据累加的情况我在用PEEKDATA时有这种情况,但用GETDATA时就没有了,我想后者自动清了缓冲区吧。
真的很感谢你。加了一点分,不好意思太少了。