请问以下文件发送和接收代码运行后,文件打不开,出现头文件被破坏,请高人指点!
发送文件:
FileStream fs1 = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br1 = new BinaryReader(fs1);//将文件流数据读做二进制值
long filelength = fs1.Length;
int times = (Int32)(filelength / 5120);
if ((long)(times * 5120) < filelength)//如果文件总长度不是5K的整数倍那么就把循环次数加以
{
times = times + 1;
}
//设置进度条
this.progressBar1.Maximum = times;
this.progressBar1.Minimum = 0;
//开始发送文件
byte[] tcp_byte = null;
int finishtimes = 0;
while (finishtimes < times)
{
try
{
if (finishtimes < times - 1)//进行前times-1次循环
{
tcp_byte = new byte[5120];
br1.Read(tcp_byte, 0, 5120);//将二进制流顺序读入字节数组以便发送
}
else //finishtimes=times-1以下执行最后一次循环
{
//最后一次所剩的数据字节数
Int32 intby = (Int32)(filelength - (long)finishtimes * 5120);
tcp_byte = new byte[intby];
br1.Read(tcp_byte, 0, tcp_byte.Length);
}
//向接收端发送5k字节的数据
Link.SendToServer(filens, tcp_byte);
for (int i = 0; i < 30000; i++)
{
//接收对方的确认信息
string content = Link.ReceiveFromServer(filens);
if ((content != "") && (i < 20000))
{
string[] strflag = content.Split(new char[] { '|' });
//如果上一段5k数据发送成功,则对方回复next|,否则等待
if (strflag[0] == "next")
{
finishtimes = finishtimes + 1;
}
this.progressBar1.Value = finishtimes;
break;
}
if (i == 20000)
{
if (finishtimes < times - 1)//进行前times-1次循环
{
tcp_byte = new byte[5120];
br1.Read(tcp_byte, 0, 5120);//将二进制流顺序读入字节数组以便发送
}
else //finishtimes=times-1以下执行最后一次循环
{
//最后一次所剩的数据字节数
Int32 intby = (Int32)(filelength - (long)finishtimes * 5120);
tcp_byte = new byte[intby];
br1.Read(tcp_byte, 0, tcp_byte.Length);
}
//向接收端发送5k字节的数据
Link.SendToServer(filens, tcp_byte);
break;
} } }
catch
{
break;
} }
this.labfile.Text = "文件发送成功!";
//关闭连接释放资源
filelisten.Stop();
filesocket.Close();
filens.Close();
}
接收文件:
string filetype = Path.GetExtension(file.filename);
this.saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + "//" + file.filename;
this.saveFileDialog1.Filter = "文件类型*" + filetype + "*|" + filetype;
this.saveFileDialog1.Title = "选择保存的路径";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs1 = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);
BinaryWriter bw1 = new BinaryWriter(fs1);
long filelength = file.filesize;
int times = (Int32)(filelength / 5120);//要循环的次数
if ((long)times * 5120 < filelength)
{
times = times + 1;
}
int finishtimes = 0;//已经循环的次数
this.progressBar1.Maximum = times;
this.progressBar1.Minimum = 0;
while (finishtimes < times)
{
byte[] tcpbyte = Link.ReceiveFromClient(NSwithClient);
bw1.Write(tcpbyte, 0, tcpbyte.Length);
bw1.Flush();
Link.SendToServer(NSwithClient, "next|");
finishtimes = finishtimes + 1;
this.progressBar1.Value = finishtimes;
}
//关闭流
bw1.Close();
bw1 = null;
fs1.Close(); fs1 = null;
MessageBox.Show("文件接收完毕!");
//关闭与发送端的流通道
NSwithClient.Close();
//this.Close();}
发送文件:
FileStream fs1 = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br1 = new BinaryReader(fs1);//将文件流数据读做二进制值
long filelength = fs1.Length;
int times = (Int32)(filelength / 5120);
if ((long)(times * 5120) < filelength)//如果文件总长度不是5K的整数倍那么就把循环次数加以
{
times = times + 1;
}
//设置进度条
this.progressBar1.Maximum = times;
this.progressBar1.Minimum = 0;
//开始发送文件
byte[] tcp_byte = null;
int finishtimes = 0;
while (finishtimes < times)
{
try
{
if (finishtimes < times - 1)//进行前times-1次循环
{
tcp_byte = new byte[5120];
br1.Read(tcp_byte, 0, 5120);//将二进制流顺序读入字节数组以便发送
}
else //finishtimes=times-1以下执行最后一次循环
{
//最后一次所剩的数据字节数
Int32 intby = (Int32)(filelength - (long)finishtimes * 5120);
tcp_byte = new byte[intby];
br1.Read(tcp_byte, 0, tcp_byte.Length);
}
//向接收端发送5k字节的数据
Link.SendToServer(filens, tcp_byte);
for (int i = 0; i < 30000; i++)
{
//接收对方的确认信息
string content = Link.ReceiveFromServer(filens);
if ((content != "") && (i < 20000))
{
string[] strflag = content.Split(new char[] { '|' });
//如果上一段5k数据发送成功,则对方回复next|,否则等待
if (strflag[0] == "next")
{
finishtimes = finishtimes + 1;
}
this.progressBar1.Value = finishtimes;
break;
}
if (i == 20000)
{
if (finishtimes < times - 1)//进行前times-1次循环
{
tcp_byte = new byte[5120];
br1.Read(tcp_byte, 0, 5120);//将二进制流顺序读入字节数组以便发送
}
else //finishtimes=times-1以下执行最后一次循环
{
//最后一次所剩的数据字节数
Int32 intby = (Int32)(filelength - (long)finishtimes * 5120);
tcp_byte = new byte[intby];
br1.Read(tcp_byte, 0, tcp_byte.Length);
}
//向接收端发送5k字节的数据
Link.SendToServer(filens, tcp_byte);
break;
} } }
catch
{
break;
} }
this.labfile.Text = "文件发送成功!";
//关闭连接释放资源
filelisten.Stop();
filesocket.Close();
filens.Close();
}
接收文件:
string filetype = Path.GetExtension(file.filename);
this.saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + "//" + file.filename;
this.saveFileDialog1.Filter = "文件类型*" + filetype + "*|" + filetype;
this.saveFileDialog1.Title = "选择保存的路径";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs1 = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);
BinaryWriter bw1 = new BinaryWriter(fs1);
long filelength = file.filesize;
int times = (Int32)(filelength / 5120);//要循环的次数
if ((long)times * 5120 < filelength)
{
times = times + 1;
}
int finishtimes = 0;//已经循环的次数
this.progressBar1.Maximum = times;
this.progressBar1.Minimum = 0;
while (finishtimes < times)
{
byte[] tcpbyte = Link.ReceiveFromClient(NSwithClient);
bw1.Write(tcpbyte, 0, tcpbyte.Length);
bw1.Flush();
Link.SendToServer(NSwithClient, "next|");
finishtimes = finishtimes + 1;
this.progressBar1.Value = finishtimes;
}
//关闭流
bw1.Close();
bw1 = null;
fs1.Close(); fs1 = null;
MessageBox.Show("文件接收完毕!");
//关闭与发送端的流通道
NSwithClient.Close();
//this.Close();}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货