Stangray
SAP THQ Apple Nintendo Google Reynolds Amazon Yahoo

 

上一篇:WCF 提高传输数据性能方法之一 (使用压缩)

在WCF中分批发送和接收数据来实现提高网络传输性能

 

服务端
private byte[] buffer = null;
private int bufferSize = 10000;
private long stream_length;
private Stream stream;

public void GetFile(string path)
{
stream
= new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
stream.Position
= 0;
stream_length
= stream.Length;
}

public byte[] GetBuffer()
{
if (buffer != null)
return buffer;
else
return null;
}

public bool ReadBuffer()
{
bool exist;
if (stream_length > 0)
{
if (stream_length > bufferSize)
{
buffer_currect
= new byte[bufferSize];
stream.Read(buffer,
0, bufferSize);
stream_length
-= bufferSize;
}
else
{
buffer_currect
= new byte[stream_length];
stream.Read(buffer,
0, (int)stream_length);
stream_length
= 0;
}
exist
= true;
}
else
{
exist
= false;
}
return exist;
}

 

 

客户端
using (DataClient client = new DataClient())
{
client.GetFile(
"e:\\docx.rar");
FileStream stream
= new FileStream("d:\\docx.rar", FileMode.CreateNew);
byte[] buffer;
while (client.ReadBuffer())
{
buffer
= client.GetBuffer();
stream.Write(buffer,
0, buffer.Length);
}
stream.Close();
client.Close();
}

 

 

posted on 2010-03-23 13:28  Stangray  阅读(1660)  评论(0编辑  收藏  举报