通过NetworkStream传输数据时出现问题 [英] A problem while transferring data over the NetworkStream

查看:207
本文介绍了通过NetworkStream传输数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用NetworkStream和TCPClient通过TCP连接发送一个大文件.客户端和服务器都是用C#编写的.

传输大小约为300KB的小文件时没有问题.我的缓冲区大小为2KB,这是协议的工作方式.

-服务器请求文件.
-客户端告诉服务器文件的大小,然后发送文件的前2KB.
-服务器要求文件的另一块-例如,从42649开始发送2KB数据-
-客户端发送块
-服务器要求更多,然后客户端发送......
-到达文件末尾时,客户端确认服务器传输已完成.

不用看代码,因为太长了,无法在此处发布,您是否可以看到可能导致此问题的任何问题?我什至尝试在接收和处理传入的数据块时使线程休眠.

关于

Hi,
I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#.

There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works.

-Server requests the file.
-Client tells the server the size of the file, and then sends the first 2KB of it.
-The server asks for another chunk of the file -like, send 2KB data, starting from 42649-
-Client sends the chunk
-Server asks for more, and client sends it...
-When the end of file is reached, the client acknowledges the server that transfer is complete.

Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data.

Regards

推荐答案

如果您真的想保留纯客户端-服务器范例,则服务器无法请求任何内容.服务器是完全被动的.如果要实现控制反转,可以,但不要将其称为客户端和服务器.您的应用程序不需要它.

—客户端:我正在发送文件:名称为N,大小为X字节;
—服务器:已确认; (从网络流开始读取,这是阻止操作);
—客户端:发送块;
—服务器:已确认; (先前的卡盘会附加到文件流中);

—客户端:发送块; (无需告诉它的最后一个,服务器从大小上知道);
—服务器:最后一个块已确认; (写入最后一个块,关闭文件流).

我建议您使用TcpListener/TcpClient.
您可以使用其他级别的网络,请参阅我过去的答案:
如何将byte []发送到另一台PC [ ^ ],
在局域网上与两个Windows应用程序进行通信. [ ^ ].

这里最重要的方面是线程.通常,服务器部分需要两个单独的线程:一个正在侦听新的连接,另一个正在遍历所有已连接的客户端,并通过对每个网络流进行读写来执行某些应用程序级协议.
在过去的答案中查看我的设计草图:
来自同一端口号的多个客户端 [
If you really want to keep to pure client-server paradigm, server cannot request anything. Server is purely passive. If you want to implement some inversion of control, it''s fine but don''t call it client and server. You application does not need it.

— Client: I''m sending a file: name is N, size is X bytes;
— Server: confirmed; (starts reading from a network stream, this is blocking operation);
— Client: sends chunk;
— Server: confirmed; (previous chuck is appended to file stream);

— Client: sends chunk; (no need to tell its the last one, server knows that from the size);
— Server: last chunk confirmed; (writes last chunk, closes file stream).

I would advice you use TcpListener/TcpClient.
You can use other levels of networking, see my past answers:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

Most important aspect here is threading. Normally, server part need two separate threads: one is listening for new connections, another one traverses all connected client and perform some application-level protocol by reading/writing from/to each network stream.
See my design sketch in my past answer here:
Multple clients from same port Number[^].

—SA


这篇关于通过NetworkStream传输数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆