无法退出通过 TCP/Sockets .net 进行多个缓冲区传输的循环
我正在使用 .Net (System.Net.Sockets) 通过 TCP/Sockets 传输数据
我在客户端使用此代码:
tc.SendBufferSize = 4096
Dim fs As New FileStream(wfile, FileMode.Open, FileAccess.Read)
Dim rijn As New RijndaelManaged
Dim cs As New CryptoStream(fs, rijn.CreateEncryptor(CreateKey(key), CreateIV(IV)), CryptoStreamMode.Read)
Dim bytesToSend(tc.SendBufferSize) As Byte
Dim numBytesRead As Integer
Do
numBytesRead = cs.Read(bytesToSend, 0, bytesToSend.Length)
tcp_str.Write(bytesToSend, 0, numBytesRead)
Loop Until numBytesRead = 0
tcp_str.Flush()
cs.Close()
在服务器端使用此代码:
Dim FS As New FileStream(CompleteFileName, FileMode.Append, FileAccess.Write)
Dim CS As New CryptoStream(FS, Rijn.CreateDecryptor(DataExchangeKey, DataExchangeIv), CryptoStreamMode.Write)
Tcp_Client.ReceiveBufferSize = 4096
Dim bytesToRead(Tcp_Client.ReceiveBufferSize) As Byte
Dim numBytesRead As Integer = 0
Do
numBytesRead = Tcp_NetStream.Read(bytesToRead, 0,Tcp_Client.ReceiveBufferSize)
CS.Write(bytesToRead, 0, numBytesRead)
Loop Until numBytesRead = 0
Console.WriteLine("Finished")
CS.FlushFinalBlock()
CS.Close()
FS.Close()
但服务器端不会离开 Do/Loop,而且它从不写入最后一个缓冲区,也从不执行 CS.FlushFinalBlock()
有任何想法为什么?
感谢您的帮助。
I'm transfering data over TCP/Sockets with the .Net (System.Net.Sockets)
I am using this code on client side :
tc.SendBufferSize = 4096
Dim fs As New FileStream(wfile, FileMode.Open, FileAccess.Read)
Dim rijn As New RijndaelManaged
Dim cs As New CryptoStream(fs, rijn.CreateEncryptor(CreateKey(key), CreateIV(IV)), CryptoStreamMode.Read)
Dim bytesToSend(tc.SendBufferSize) As Byte
Dim numBytesRead As Integer
Do
numBytesRead = cs.Read(bytesToSend, 0, bytesToSend.Length)
tcp_str.Write(bytesToSend, 0, numBytesRead)
Loop Until numBytesRead = 0
tcp_str.Flush()
cs.Close()
And This code on server side :
Dim FS As New FileStream(CompleteFileName, FileMode.Append, FileAccess.Write)
Dim CS As New CryptoStream(FS, Rijn.CreateDecryptor(DataExchangeKey, DataExchangeIv), CryptoStreamMode.Write)
Tcp_Client.ReceiveBufferSize = 4096
Dim bytesToRead(Tcp_Client.ReceiveBufferSize) As Byte
Dim numBytesRead As Integer = 0
Do
numBytesRead = Tcp_NetStream.Read(bytesToRead, 0,Tcp_Client.ReceiveBufferSize)
CS.Write(bytesToRead, 0, numBytesRead)
Loop Until numBytesRead = 0
Console.WriteLine("Finished")
CS.FlushFinalBlock()
CS.Close()
FS.Close()
But the server side doesn't leave the Do/Loop, futhermore it never writes the last buffer and never does the CS.FlushFinalBlock()
Any Ideas Why ?
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在客户端关闭连接之前,服务器不会离开
Do
循环(一旦数据全部消耗完,允许Read
返回 0)。因此,我怀疑您只需要(在客户端代码的末尾):(刷新流与正式关闭流非常不同)
如果客户端不这样做获取到
tcp_str.Close()
,然后检查cs
(无论cs
是什么)也可以 检测传入流的结尾。请注意,如果您使用了
Using
,所有这一切都会自动发生 - 如果您只是释放套接字(未处理),那么它将保持打开状态,直到下一次垃圾收集触发(并开始运行终结器)。The server won't leave the
Do
loop until the client closes their connection (allowingRead
to return 0 once the data is all consumed). I suspect, therefore, you just need (at the end of the client code):(flushing a stream is very different to formally closing it)
If the client doesn't get to
tcp_str.Close()
, then check thatcs
(whatevercs
is) can also detect the end of the incoming stream.Note that all of this would have happened automatically if you had used
Using
- if you just let go of the socket (undisposed) then it will remain open until garbage-collection next fires (and gets around to running the finalizer).