如何从客户端向服务器发送大文件? [英] How to send large file from client to server?

查看:141
本文介绍了如何从客户端向服务器发送大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我想使用C / C ++ Websocket从客户端向服务器发送大文件(> 100Mb)。

首先,我将文件分成几个小数据包(每个数据包<= 1500字节)。然后我将数据包发送到服务器。服务器收到数据包后,开始将数据写入磁盘。但我发现发送文件的总时间太慢了。

还有另一个发送文件的解决方案吗?

对不起我的英文!

Hi everyone.
I want send a large file (>100Mb) from client to server using C/C++ Websocket.
First, I split the file into several small packet (each packet <= 1500 bytes). Then i send the packet to server. After server received the packet then start write data to disk. But i see the total time to send the file is too slow.
Is there another solution to send file?
Sorry for my english!

推荐答案

检查您的实现是否有最佳数据包大小,并确保您没有为每个数据包建立新连接。



我发现了这个< a href =http://vichargrave.com/network-programming-design-patterns-in-c/>精美示例或 Linux



提示:使用不同的数据包大小。我的提示是,1024字节将更快,因为数据包数量正在减少。 1500不是最佳的:1024 + 476 => 1包仅半满。



当然还有其他方式:将其复制到USB-Stick上并通过邮政服务发送。 ; - )
Check your implementation for optimal packet size and ENSURE that you dont make a new connection for EVERY packet.

I found this fine example or this for Linux.

Tip: play around with different packet sizes. My tip is, that 1024 bytes will go faster, because the packet amount is decreasing. 1500 not optimal: 1024 + 476 => 1 packet only half full.

"Ofcourse are there other ways": copy it on a USB-Stick and send it via postal service,. ;-)


使用队列来处理所有传输请求。当您阅读要发送的文件时,请快速将其添加到队列中。将队列作为一个单独的线程处理,实际上将这些块发送到服务器。

尽量使用变量(或设置)设置块大小来简化流程并使其可自定义这样,在测试时,您可以轻松更改这些设置并相应地测试结果。
Use a queue to handle all transport request. When you read the file you wish to send, go over it quickly adding chunks to the queue. Have the queue handled as a separate thread which will actually send these chunks to the server.
Try to simplify the process and make it customizable as possible, using variables (or "settings") to set chunk size, etc. That way, while testing it, you can easily change these settings and test the results accordingly.


首先,我希望您使用流式传输...暗示内核将处理低级碎片。话虽如此,数据包通常根据MTU大小进行分段。 Linux中的默认MTU大小为1500字节。如果你的软件碎片大小小于那个并通过TCP流式传输,猜猜会发生什么......内核将在传输之前等待更多数据。换句话说,在应用层碎片到任何小于MTU大小的东西只是浪费时间。



理想的应用层碎片应该是:

MTU> =片段< sock_buff_size



无法调用默认的套接字缓冲区大小,但它们是可配置的。一旦套接字缓冲区已满,send()应该阻塞,直到空间打开(除非你指定了非阻塞)。您的应用程序层碎片大小应该小于总缓冲区大小的一半(至少,您不想创建send()备份。与双缓冲相同的想法。)
First off, I hope you're using streaming... implying the kernel will handle low level fragmentation. With that said, packets are usually fragmented according to the MTU size. The default MTU size in Linux is 1500 bytes. If your software is fragmenting at sizes smaller than that and streaming that over TCP, guess what's going to happen... the kernel is going to wait for more data before transmitting that. In another words, fragmenting at the application layer to anything less than the MTU size is just wasting time.

Your ideal application layer fragmentation should be:
MTU >= fragment < sock_buff_size

Can't recall default socket buffer sizes but they are configurable. Once the socket buffer is full, send() should block until space opens up (unless you specified non-blocking). Your application layer fragmentation size should probably be smaller than half of the total buffer size (at least, you don't want to create send() backups.. same idea as "double buffering").


这篇关于如何从客户端向服务器发送大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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