我有一台服务器可以向请求它们的客户端发送文件。对于大小小于2演出的文件,它的效果非常好。 但是,当文件超过2演出时,我的服务器会在 Dim buffer 处给我一个溢出错误。我找了它,发现缓冲区大小可能是(upload.SendBufferSize -1)。但是,更改此项会给我一个不同的错误: Upload.Send :无法立即完成非阻塞套接字操作。
我从互联网上搜索过,一个建议是尝试添加Upload.Blocking = True如果发生错误,但后来我收到一个错误,说明参数无效。
对我的问题有任何建议吗?
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
答案 0 :(得分:0)
好吧,我自己得到了,如果有人需要它,你不能通过从异步变为同步来做到这一点。只需使用:
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