通过监视发现传输中Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    ReDim Buff(1 To bytesTotal)
    Winsock1.GetData Buff, vbByte
    CopyMemory ShowBuff(Num + 1), Buff(1), bytesTotal
    Num = Num + bytesTotal
End Sub会使得到的数据与发送数据不一致(数组错位)
建议重写该段代码

解决方案 »

  1.   

    解决了!Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
            (pDst As Any, pSrc As Any, ByVal ByteLen As Long)Private Const BI_RGB = 0&
    Private Const DIB_RGB_COLORS = 0Private Type BITMAPINFOHEADER
            biSize As Long
            biWidth As Long
            biHeight As Long
            biPlanes As Integer
            biBitCount As Integer
            biCompression As Long
            biSizeImage As Long
            biXPelsPerMeter As Long
            biYPelsPerMeter As Long
            biClrUsed As Long
            biClrImportant As Long
    End Type
    Private Type RGBQUAD
            rgbBlue As Byte
            rgbGreen As Byte
            rgbRed As Byte
            rgbReserved As Byte
    End Type
    Private Type BITMAPINFO
            bmiHeader As BITMAPINFOHEADER
            bmiColors As RGBQUAD
    End Type
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
    Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As LongDim iBitmap As Long, IDC As Long
    Dim bi24BitInfo As BITMAPINFO, bBytes() As Byte, Cnt As Long
    Dim Num As Long, Buff() As Byte, ShowBuff() As Byte
    Private Sub Form_Load()
        With bi24BitInfo.bmiHeader
            .biBitCount = 24
            .biCompression = BI_RGB
            .biPlanes = 1
            .biSize = Len(bi24BitInfo.bmiHeader)
            .biWidth = Screen.Width \ Screen.TwipsPerPixelX    '15改为Screen.TwipsPerPixelX
            .biHeight = Screen.Height \ Screen.TwipsPerPixelY  '15改为Screen.TwipsPerPixelY
        End With
        
        ReDim ShowBuff(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte
        ReDim bBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte  '24位色
        
        
        IDC = CreateCompatibleDC(0)
        iBitmap = CreateDIBSection(IDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
        SelectObject IDC, iBitmap
        BitBlt IDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, GetDC(0), 0, 0, vbSrcCopy
        GetDIBits IDC, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, bBytes(1), bi24BitInfo, DIB_RGB_COLORS
        
        Winsock1.LocalPort = 5458
        Winsock1.Bind 5458
        Winsock1.Listen
        
        Winsock2.RemoteHost = Winsock1.LocalIP
        Winsock2.RemotePort = 5458
        Winsock2.Connect Winsock2.RemoteHost, 5458
    End SubPrivate Sub Command1_Click()
        Num = 0
        Winsock2.SendData bBytes
    End SubPrivate Sub Command2_Click()
        Me.Picture = LoadPicture("")    SetDIBitsToDevice Me.hdc, 0, 0, bi24BitInfo.bmiHeader.biWidth, _
            bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, bi24BitInfo.bmiHeader.biHeight, _
            ShowBuff(1), bi24BitInfo, DIB_RGB_COLORS
    End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
        If Winsock1.State <> sckClosed Then Winsock1.Close
        Winsock1.Accept requestID
    End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        ReDim Buff(1 To bytesTotal)
        Winsock1.GetData Buff, vbByte
        CopyMemory ShowBuff(Num + 1), Buff(0), bytesTotal  'Buff(1)改为Buff(0)
        Num = Num + bytesTotal
    End Sub
      

  2.   

    DIB位图的的大小计算错了        Select Case CBit
            Case 1
                .biSizeImage = (((.biWidth + 7) \ 8 + 3) And &HFFFFFFFC) * .biHeight
            Case 4
                .biSizeImage = (((.biWidth + 1) \ 2 + 3) And &HFFFFFFFC) * .biHeight
            Case 8
                .biSizeImage = ((.biWidth + 3) And &HFFFFFFFC) * .biHeight
            Case 16
                .biSizeImage = ((.biWidth * 2 + 3) And &HFFFFFFFC) * .biHeight
            Case 24
                .biSizeImage = ((.biWidth * 3 + 3) And &HFFFFFFFC) * .biHeight
            Case 32
                .biSizeImage = .biWidth * 4 * .biHeight
            Case Else
                Exit Function
            End Select
            
      

  3.   

    根据junwhj所说的,ReDim Buff(1 To bytesTotal)这条语句没有起作用,也就是说把这条语句删除也可以正常运行,这是为什么?
      

  4.   

    因为CopyMemory的第二个参数是一个内存块的开始地址呀!
      

  5.   

    可是我已经ReDim Buff(1 To bytesTotal)了,没有Buff(0)啊
      

  6.   

    你在通用部分用了Buff() As Byte,它就是一个数组了,
    Winsock1.GetData Buff, vbByte这一句向数组写入了内容,
    怎么会没有Buff(0)?
      

  7.   

    ====================================================================
    这段写得很精彩,但只能单机运行。将它分开,它会在“    CopyMemory ShowBuff(Num + 1), Buff(0), bytesTotal  'Buff(1)改为Buff(0)
    ”这句出错!!!能将它改为真正可在网络运行的程序吗?
    ===============热切期望=============================================