【问题标题】:VB Socket.Send returns "A non blocking socket operation could not be completed immediately."VB Socket.Send 返回“无法立即完成非阻塞套接字操作。”
【发布时间】:2017-01-03 10:24:08
【问题描述】:

我有一台服务器,可以将文件发送给请求它们的客户端。它适用于小于 2 gigs 的文件。 但是,当文件超过 2 个演出时,我的服务器会在 Dim 缓冲区 处给我一个溢出错误。我找了一下,发现缓冲区大小可能是(upload.SendBufferSize -1)。但是,改变它会给我一个不同的错误: Upload.Send无法立即完成非阻塞套接字操作。

我从互联网上搜索了它,一个建议是尝试添加 Upload.Blocking = True 如果发生错误,但随后我收到一个错误,提示 Argument is invalid。

对我的问题有什么建议吗?

Private Sub UploaderAccept(ar As IAsyncResult)
    Dim File As IO.FileInfo = ar.AsyncState
    Dim Upload As Socket = UploadServer.EndAccept(ar)

    '--- Determine size of buffer
    Dim fs As New IO.FileStream(File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim buffer(Upload.SendBufferSize - 1) As Byte
    Dim Sent As Long = 0
    Dim t As Long = 0

    '--- Actual sending
    Do Until Sent >= File.Length
        t = fs.Read(buffer, 0, buffer.Length)
        Upload.Send(buffer, 0, t, SocketFlags.None)

        Sent += t
    Loop

    '--- File transfer done
    fs.Close()

    Upload.BeginDisconnect(False, New AsyncCallback(AddressOf OnDisconnect), upload)
    Upload.Close()
End Sub

【问题讨论】:

    标签: file buffer send blocking transfer


    【解决方案1】:

    好的,我自己搞定了,如果有人需要,你不能通过从异步更改为同步来做到这一点。只需使用:

    Upload.BeginSend(buffer, 0, t, SocketFlags.None, New AsyncCallback(AddressOf OnSend), Upload):

        Do Until Sent >= File.Length
            t = fs.Read(buffer, 0, buffer.Length)
            If t > 0 Then
                Upload.BeginSend(buffer, 0, t, SocketFlags.None, New AsyncCallback(AddressOf UploadOnSend), Upload)
            End If
    
            Sent += t
            TextUpdate(lblFilePerc, Sent)
        Loop
        Close streams...
    
    Private Sub UploadOnSend(ar As IAsyncResult)
        Dim upload As Socket = ar.AsyncState
        upload.EndSend(ar)
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多