客户端:
DialogResult dr=openFileDialog1.ShowDialog();
if (DialogResult.OK == dr)
{
Stream st = openFileDialog1.OpenFile();
byte[] by = new byte[1000];
int count = Convert.ToInt32(st.Length);//获取文件流长度
while (count != 0)//循环写入
{
if (count - by.Length < 0)//判断流长度 如果小于1000就按照剩余流量写入 否则按1000写入
{
st.Read(by, 0, count);//读取流 按照剩余流量读取
ns.Write(by, 0, count);//写入网络流中
st.Flush();//刷新文件流
ns.Flush();//刷新网络流
count = 0;//已经读取完毕 设置为0 停止循环
}
else
{
count = count - by.Length;//将原有流减去已经发送的流
st.Read(by, 0, by.Length);//读取流
ns.Write(by, 0, by.Length);//写入网络流中
st.Flush();
ns.Flush();
} }
st.Close();
MessageBox.Show("传送完毕");
}
服务器:
if (ns.DataAvailable)
{
DialogResult rs = saveFileDialog1.ShowDialog();
if (DialogResult.OK == rs)
{
byte[] by = new byte[1000];
int count = 0;
FileStream ft = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
do//循环读取 直到网络流为0
{ count = cl.Available;//检查可以数据流
if (count < by.Length)//判断流长度 如何小于数组长度就按照count的长度读取
{ //和上面的差不多意思
ns.Read(by, 0, count );
ft.Write(by, 0, count );
ns.Flush();
ft.Flush();
}
else
{
ns.Read(by, 0, by.Length);
ft.Write(by, 0, by.Length);
ns.Flush();
ft.Flush(); }
} while (count != 0); ft.Close();
MessageBox.Show("接受完毕!");
}
大家觉得这个代码怎么样!弊端在哪里?(不能够传送大文件)发表一下你们的意见吧!
DialogResult dr=openFileDialog1.ShowDialog();
if (DialogResult.OK == dr)
{
Stream st = openFileDialog1.OpenFile();
byte[] by = new byte[1000];
int count = Convert.ToInt32(st.Length);//获取文件流长度
while (count != 0)//循环写入
{
if (count - by.Length < 0)//判断流长度 如果小于1000就按照剩余流量写入 否则按1000写入
{
st.Read(by, 0, count);//读取流 按照剩余流量读取
ns.Write(by, 0, count);//写入网络流中
st.Flush();//刷新文件流
ns.Flush();//刷新网络流
count = 0;//已经读取完毕 设置为0 停止循环
}
else
{
count = count - by.Length;//将原有流减去已经发送的流
st.Read(by, 0, by.Length);//读取流
ns.Write(by, 0, by.Length);//写入网络流中
st.Flush();
ns.Flush();
} }
st.Close();
MessageBox.Show("传送完毕");
}
服务器:
if (ns.DataAvailable)
{
DialogResult rs = saveFileDialog1.ShowDialog();
if (DialogResult.OK == rs)
{
byte[] by = new byte[1000];
int count = 0;
FileStream ft = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
do//循环读取 直到网络流为0
{ count = cl.Available;//检查可以数据流
if (count < by.Length)//判断流长度 如何小于数组长度就按照count的长度读取
{ //和上面的差不多意思
ns.Read(by, 0, count );
ft.Write(by, 0, count );
ns.Flush();
ft.Flush();
}
else
{
ns.Read(by, 0, by.Length);
ft.Write(by, 0, by.Length);
ns.Flush();
ft.Flush(); }
} while (count != 0); ft.Close();
MessageBox.Show("接受完毕!");
}
大家觉得这个代码怎么样!弊端在哪里?(不能够传送大文件)发表一下你们的意见吧!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货