当前位置: 行情首页 >> 技术文章 >> 技术文章 >> 请注意:网络winsock控件实

  • 请注意:网络winsock控件实现数据传输的问题! VB / 控件
  • 2007-10-18 15:40:32 杭州电脑数码城 转载来源:csdn.net
  • 社区 - VB / 控件

    请注意:网络winsock控件实现数据传输的问题!

    () 2000-02-01 12:02:00在 VB / 控件 提问

    我想通过Winsock控件,在服务机与客户机之间传递文件,不知用什么方法实现.
    Winsock控件的SendDate和GetDate方法只能支持发送接受字符串和字节数组.
    不知各位有什么见解,但说无妨.



    问题点数:0、回复次数:10

    1楼 littletao () 回复于 2000-02-01 12:11:00 得分 0


    读取2进制文件后放在字节数组里不就可以了?

    2楼 zm (小聪明) 回复于 2000-02-01 15:09:00 得分 0


    我当时也遇到过这种问题,我就是用了lilltetao的方法,将文件先以二进制方式打开,然后Read到一个数组,再用senddata发送,getdata接收后再write到一个二进制文件中,但是用此方法传送一个位图文件后不成功,文件头有错误(好象与空格有关系)。我觉的此种方法在理论上是可行的,多调试几次应该能成功

    3楼 () 回复于 2000-02-03 09:10:00 得分 0


    littletao is right,

    4楼 () 回复于 2000-02-04 00:52:00 得分 0


    也许你应该先定义要发送字符数组的大小:首先用filelen函数确定要发送文件大小,
    然后用redim重新定义字符数组的大小,ok。

    5楼 Firing_Sky (火的天空) 回复于 2000-02-04 13:15:00 得分 0


    只要允许字节数组,还有什么不能干的?

    6楼 () 回复于 2000-02-05 10:58:00 得分 0


    1月31日《计算机世界报》上有一篇文章,是delphi的,但可参考。

    7楼 zhangdr () 回复于 2000-03-17 13:23:00 得分 0


    PutFile 用来传送文件,数据流的格式是:第一个字节代表传送的是文件(因为在我的程序中我还要传送确认,用这个字节以示区分);第二个字节代表文件名长度,以后是文件名、数据块长度、数据块。
    Dim bRecieved As Boolean
    Private Sub tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim byteData() As Byte, rsResult As ADODB.Recordset

    If bytesTotal > 0 Then
    ReDim byteData(bytesTotal - 1)
    tcpServer(Index).GetData byteData, vbByte + vbArray

    Select Case byteData(0)
    Case 0
    bRecieved = True
    Case 1
    SaveFile byteData
    tcpServer(Index).SendData CByte(0)
    End Select
    End If
    End Sub
    Private Sub PutFile(objSendSock As Winsock, sFileName As String)
    Dim iThisFile As Integer, byteData() As Byte
    Dim i As Long, j As Long, byteBuffer() As Byte
    Dim iBlockCount As Integer, lDivValue As Long

    ' On Error GoTo errPutFile
    iThisFile = FreeFile
    Open sFileName For Binary As #iThisFile
    ReDim byteData(Len(sFileName) + 3)
    byteData(0) = 1
    byteData(1) = CByte(Len(sFileName))
    For j = 1 To Len(sFileName)
    byteData(j + 1) = CByte(Asc(Mid(sFileName, j, 1)))
    Next
    iBlockCount = LOF(iThisFile) \ BufferSize
    For i = 0 To 1
    byteData(i + 2 + Len(sFileName)) = CByte((iBlockCount \ 2 ^ (i * 8)) And &HFF)
    Next
    For i = 1 To iBlockCount
    ReDim Preserve byteData(BufferSize + 10 + Len(sFileName))
    For j = 0 To 1 '计算当前帧号
    byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
    Next
    For j = 0 To 3 '计算数据块长度
    byteData(j + 6 + Len(sFileName)) = CByte((BufferSize \ 2 ^ (j * 8)) And &HFF)
    Next
    ReDim byteBuffer(BufferSize - 1)
    Get #iThisFile, , byteBuffer
    For j = 0 To BufferSize - 1
    byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
    Next
    bRecieved = False
    objSendSock.SendData byteData
    Do While Not bRecieved
    DoEvents
    Loop
    Next
    lDivValue = LOF(iThisFile) Mod BufferSize
    If lDivValue <> 0 Then
    ReDim Preserve byteData(lDivValue + 10 + Len(sFileName))
    For j = 0 To 1
    byteData(j + 4 + Len(sFileName)) = CByte((i \ 2 ^ (j * 8)) And &HFF)
    Next
    For j = 0 To 3
    byteData(j + 6 + Len(sFileName)) = CByte((lDivValue \ 2 ^ (j * 8)) And &HFF)
    Next
    ReDim byteBuffer(lDivValue - 1)
    Get #iThisFile, , byteBuffer
    For j = 0 To lDivValue - 1
    byteData(j + 10 + Len(sFileName)) = byteBuffer(j)
    Next
    bRecieved = False
    objSendSock.SendData byteData
    Do While Not bRecieved
    DoEvents
    Loop
    End If
    Close #iThisFile
    Exit Sub
    errPutFile:
    Select Case Err.Number
    Case Else
    MsgBox Err.Number & ":" & Err.Description, vbInformation, "PutFile 错误提示"
    End Select
    End Sub

    Private Sub SaveFile(byteData() As Byte)
    Dim lSize As Long, i As Integer, strTemp As String
    Dim sFileName As String, byteBuffer() As Byte
    Dim iThisFile As Integer, lSoFarIn As Long
    Dim iDotPos As Integer
    Dim iBlockIndex As Integer, iBlockCount As Integer

    'On Error GoTo errSaveData
    sFileName = ""
    For i = 1 To byteData(1)
    sFileName = sFileName & Chr(byteData(i + 1))
    Next
    For i = 0 To 1
    iBlockCount = iBlockCount + byteData(i + 2 + byteData(1)) * 2 ^ (i * 8)
    Next
    For i = 0 To 1
    iBlockIndex = iBlockIndex + byteData(i + 4 + byteData(1)) * 2 ^ (i * 8)
    Next
    If iBlockIndex = 1 Then
    i = 0
    strTemp = sFileName
    Do While Dir(strTemp) <> ""
    i = i + 1
    strTemp = sFileName
    iDotPos = InStr(strTemp, ".")
    strTemp = Left(strTemp, iDotPos - 1) & "_" & i & Right(strTemp, Len(strTemp) - iDotPos + 1)
    Loop
    If Dir(sFileName) <> "" Then
    FileCopy sFileName, strTemp
    Kill sFileName
    End If
    End If
    For i = 0 To 3
    lSize = lSize + byteData(i + byteData(1) + 6) * 2 ^ (i * 8)
    Next
    ReDim byteBuffer(lSize - 1)
    For i = 0 To lSize - 1
    byteBuffer(i) = byteData(i + byteData(1) + 10)
    Next
    iThisFile = FreeFile
    Open sFileName For Binary As iThisFile
    lSoFarIn = LOF(iThisFile) + 1
    Put #iThisFile, lSoFarIn, byteBuffer
    Close #iThisFile
    Exit Sub
    errSaveData:
    Select Case Err.Number
    Case Else
    MsgBox Err.Number & ":" & Err.Description, , "SaveFile 错误提示"
    End Select
    End Sub

    8楼 zhangdr () 回复于 2000-03-17 13:27:00 得分 0


    对不起,我忘了定义BufferSize了,BufferSize不能太大
    Const BufferSize = 2 * 1024

    9楼 apollo (古秋) 回复于 2000-03-19 16:36:00 得分 0


    关注..

竟价广告:

    业界行情新闻声明事项:

    • ☉本网转载出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性!
    • ☉如其他媒体、网站或个人从本网下载使用,必须保留本网注明的“稿件来源”,并自负版权等法律责任。如对稿件内容有疑议,请及时与我们联系.
    • ☉如本网转载稿涉及版权等问题,请作者在速来电或来函与杭州电脑数码城网联系.
    • ☉本站网址:http://www.ititt.com/投诉邮箱:6371222@qq.com