无法退出通过 TCP/Sockets .net 进行多个缓冲区传输的循环

发布于 2024-12-12 06:08:17 字数 1255 浏览 0 评论 0原文

我正在使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2024-12-19 06:08:17

在客户端关闭连接之前,服务器不会离开 Do 循环(一旦数据全部消耗完,允许 Read 返回 0)。因此,我怀疑您只需要(在客户端代码的末尾):(

tcp_str.Close()

刷新流与正式关闭流非常不同)

如果客户端不这样做获取tcp_str.Close(),然后检查cs(无论cs是什么)也可以 检测传入流的结尾。

请注意,如果您使用了 Using,所有这一切都会自动发生 - 如果您只是释放套接字(未处理),那么它将保持打开状态,直到下一次垃圾收集触发(并开始运行终结器)。

The server won't leave the Do loop until the client closes their connection (allowing Read to return 0 once the data is all consumed). I suspect, therefore, you just need (at the end of the client code):

tcp_str.Close()

(flushing a stream is very different to formally closing it)

If the client doesn't get to tcp_str.Close(), then check that cs (whatever cs 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文