这个是报错:无法将数据写入传输连接: 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。。 if (result.Equals(string.Empty))
{
try
{
byte[] buf = File.ReadAllBytes(filePath);//filePath文件路劲
SendData(buf, HostIP);//HostIP IP地址
Console.WriteLine("发送成功!");
}
catch (Exception e)
{
Console.WriteLine("错误:" + e.ToString());
continue;
}
}void SendData(byte[] buf, string hostIP)
{
try
{
Thread.Sleep(1000);
TcpClient tc = new TcpClient();
tc.Connect(IPAddress.Parse(hostIP), 2000);
NetworkStream ns = tc.GetStream();
ns.Write(buf, 0, buf.Length);
ns.Flush();
ns.Close();
tc.Close();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
}上面是我的程序一部分,请教一下为什么总是报错,不是说tcp可以自己分开段来传输吗?在网上查过后有思路说是把一个文件分成几块分别传输,但是怎么实现一直搞不明白,C#初学者请大家指教,谢谢!
{
try
{
byte[] buf = File.ReadAllBytes(filePath);//filePath文件路劲
SendData(buf, HostIP);//HostIP IP地址
Console.WriteLine("发送成功!");
}
catch (Exception e)
{
Console.WriteLine("错误:" + e.ToString());
continue;
}
}void SendData(byte[] buf, string hostIP)
{
try
{
Thread.Sleep(1000);
TcpClient tc = new TcpClient();
tc.Connect(IPAddress.Parse(hostIP), 2000);
NetworkStream ns = tc.GetStream();
ns.Write(buf, 0, buf.Length);
ns.Flush();
ns.Close();
tc.Close();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
}上面是我的程序一部分,请教一下为什么总是报错,不是说tcp可以自己分开段来传输吗?在网上查过后有思路说是把一个文件分成几块分别传输,但是怎么实现一直搞不明白,C#初学者请大家指教,谢谢!
那请问,如果我这边拆开了,接收端传过去的还是一个文件吗?或者是这边拆开传,那边要怎么接受?
fileStream.Write(buf, 0, flag);buf = new byte[1024];flag = networkStream.Read(buf, 0, buf.Length);
的话可以么?
用while 读取一定的文件长度:(查下readByte 的用法吧)
每次send 一下就行了.(这个你会的)
注意一下,
要自已定一个规则(即协议) 如何表示这次发现的数据是什么数据,才能进行下一步操作,这个你要好好想想。是消息还是文件,是文件的哪一段? 要自已定协议!
using (FileStream sf = new FileStream(FileName, FileMode.Append))//FileName:是文件名:(要加入数据的)
{
sf.Write(receiveByte, 0, receiveByte.length); //receiveByte 是接收到的数组 提取出的文件部分,
rtbShow.AppendText(sfd.FileName+"保存成功!");
}
{
sf.Write(receiveByte, 0, receiveByte.length); //receiveByte 是接收到的数组 提取出的文件部分,
//这上面的代码,你可以 封装成方法
///文件名:FileName
///数据byte[] :receiveByte //要追加的数据
}