插座传输速度 [英] Socket transfer speed
本文介绍了插座传输速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经进行了简单的服务器/客户端文件传输,
Uploader的上传带宽为1MB/s,下载器的下载带宽为12MB/s.
然而,最大传输速度约为200kB/s,因此潜力只有其五分之一.
有什么办法可以提高它的性能?
I have made a simple Server/Client file transfer,
The Uploader has a 1MB/s upload bandwidth and downloader 12MB/s download bandwith.
Yet the max transfer speed is around 200kB/s, so a 5th of it''s potential.
Is there anything that can be done to increase it''s performance?
//File path in string file
FileInfo fi = new FileInfo(file);
int len = 0;
int rdby = 0;
FileStream fin = new FileStream(file, FileMode.Open, FileAccess.Read);
NetworkStream nfs = new NetworkStream(p);
//Send Header
byte[] tmp = new byte[9];
//File length
BitConverter.GetBytes(fi.Length).CopyTo(tmp, 1);
//Command byte
tmp[0] = (byte)Cmd.download;
nfs.Write(tmp, 0, tmp.Length);
//Send File, pSize = 1024
tmp = new byte[pSize];
while (rdby < fi.Length && nfs.CanWrite)
{
len = fin.Read(tmp, 0, tmp.Length);
nfs.Write(tmp, 0, len);
rdby = rdby + len;
}
fin.Close();
感谢您的阅读.
Thanks for reading.
推荐答案
我现在只能看到两种可能性:1)nfs.CanWrite
过多(但几乎不会影响您的吞吐量),2)您的缓冲区大小不是最佳.
但是,您不能在代码中使用任意大小的缓冲区来提高性能.您还应该检查系统中配置的最大缓冲区大小.请参阅 http://msdn.microsoft.com/en-us/library/ms819736.aspx [ ^ ].
—SA
I can see only two possibilities right now: 1)nfs.CanWrite
is excessive (but hardly could affect your throughput much), 2) your buffer size is not optimal.
However, you cannot use arbitrary-size buffer in your code and improve performance. You should also check the maximum buffer size configured in your system. See http://msdn.microsoft.com/en-us/library/ms819736.aspx[^].
—SA
这篇关于插座传输速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文