如何等待流写入完成
我有一个服务器应用程序,用于侦听端口 8888 上的连接。我还在创建客户端应用程序。这是一个简单的应用程序,唯一的困难是管理多个连接。所以我只需要在计算机之间发送文件,所以我这样做的方式,我不知道它是否正确,但它有效也许你们可以纠正我。这是发送文件时的算法:
NetworkStream stream = \\ initialize it
while(someCondition)
{
// first I open the file for reading and read chunks of it
byte[] chunk = fileRead(file, indexStart, indexEnd) // I have a similar method this is just to illustate my point
stream.Write(chunk, \\other params)
// since I often send large files it will be nice if I can wait here
// until the stream.Write is done. when debuging this the while loop
// executes several times then it waits.
}
在另一边,我从该流中读取字节并将其写入文件。
有时我还需要等待,因为我发送了多个文件,并且我想确保第一个文件已发送,然后再移动到下一个文件。我知道传输完成后我可以通过使用stream.Read方法来解决这个问题。并从客户端发回数据。但有时我相信知道stream.write何时完成会很有帮助。
编辑
好,根据您的答案,我可以向客户端发送我计划发送的字节数。一旦客户端收到这么多字节,就意味着它已经完成。但我的问题是这是否有效。 做类似的事情
我的意思是在服务器上
:写入数据“发送文件长度”
读取数据“检查客户端是否收到长度”(例如期望字符串正常)
写入数据“告诉客户端文件的名称”
读数据“检查客户端是否接收到文件名”
写数据“开始发送文件块”
读数据“等待客户端回复字符串 ok”
I have a server app that listens for connections on port 8888. I am also creating the client application. It is a simple application the only hard thing about it is managing multiple connections. so I just need to send files between computers so the way I do that, I don't know if it is wright but it works maybe you guys can correct me. here is my algorithm when sending the file:
NetworkStream stream = \\ initialize it
while(someCondition)
{
// first I open the file for reading and read chunks of it
byte[] chunk = fileRead(file, indexStart, indexEnd) // I have a similar method this is just to illustate my point
stream.Write(chunk, \\other params)
// since I often send large files it will be nice if I can wait here
// until the stream.Write is done. when debuging this the while loop
// executes several times then it waits.
}
and on the other side I read bytes from that stream and write it to a file.
I also need to wait sometimes because I send multiple files and I want to make sure that the first file has been sent before moving to the next. I know I can solve this by using the stream.Read method once the transfer has been done. and sending data back from the client. but sometimes I believe it will be helpful to know when the stream.write is done.
Edit
ok so based on your answers I can for example send to the client the number of bytes that I am planing to send. and once the client recives that many bytes it means it is done. But my question is if this is efficient. I mean doing something like
on the server:
writing data "sending the file length"
read data "check to see if the client received the length" (expecting a string ok for example)
write data "tel the client the name of the file"
read data "check to see if the client recived the name of the file"
write data "start sending chuncks of the file"
read data "wait until client replies with the string ok for example"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当该行完成时,写入完成
。值得注意的是,这并不意味着另一端已收到任何内容。事实上,紧接该行之后,数据可能位于发送计算机上的某个缓冲区中。这意味着它现在超出了你的控制范围。如果您想要确认收据,远程端必须通知您。
The write is complete when the line
completes. It's worth noting that this does not imply that the other end has received anything. In fact, immediately subsequent to that line, the data is likely to be in some buffer on the sending machine. That means that it's now out of your control. If you want receipt confirmation, the remote end will have to let you know.
Stream.Write 是同步的,因此它将始终阻塞您的线程,直到写入完成。
Stream.Write is synchronous, so it will always block your thread until the writing finishes.