小弟在做发送大文件,分步发送,1M1M的发送,在byte前都加行了标识1表示发送文件,0表示信息,但是问题出现了,接收的时候第1次是以1开头的byte的数组,第2次居然不是了,小弟找不到问题的所在,求帮助。发送文件代码:public void SendFile(string strMsg) //发送文件
{
using (FileStream fs = new FileStream(strMsg, FileMode.Open))
{
byte[] byFile = new byte[1024 * 1024 * 1];
while (true)
{
int bylength = fs.Read(byFile, 0, byFile.Length);
byte[] by2 = new byte[bylength + 1];
by2[0] = 1;
Buffer.BlockCopy(byFile, 0, by2, 1, bylength);
socket.Send(by2);
if (bylength < byFile.Length)
{
break;
}
}
msg("文件:你对" + socket.RemoteEndPoint.ToString() + " 发送文件" + strMsg);
}
}
接收文件代码: FileStream fs = null;
void Receive() //接收消息
{
try
{
while (isOnlie)
{
byte[] by = new byte[1024 * 1024 * 1];
int socLength = clientSocket.Receive(by);
if (by[0] == 0)
{
AppendLine("消息:服务端对你说 " + System.Text.Encoding.Default.GetString(by, 1, socLength - 1));
}
else if (by[0] == 1)
{
if (isSave)
{
fs.Write(by, 1, socLength - 1);
if (socLength < by.Length)
{
fs.Flush();
fs.Dispose();
isSave = false;
AppendLine("文件:你的文件地址为:" + FileName);
FileName = string.Empty;
break;
}
}
else
{
SaveFileDialog sd = new SaveFileDialog();
if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileName = sd.FileName;
fs = new FileStream(FileName, FileMode.OpenOrCreate);
fs.Write(by, 1, socLength - 1);
isSave = true;
if (socLength < by.Length)
{
fs.Flush();
fs.Dispose();
isSave = false;
AppendLine("文件:你的文件地址为:" + FileName);
FileName = string.Empty;
break;
}
}
}
}
}
}
catch (Exception ex)
{
btnLianJie.Text = "连接";
AppendLine("连接:服务端下线了");
clientSocket.Dispose();
}
}
{
using (FileStream fs = new FileStream(strMsg, FileMode.Open))
{
byte[] byFile = new byte[1024 * 1024 * 1];
while (true)
{
int bylength = fs.Read(byFile, 0, byFile.Length);
byte[] by2 = new byte[bylength + 1];
by2[0] = 1;
Buffer.BlockCopy(byFile, 0, by2, 1, bylength);
socket.Send(by2);
if (bylength < byFile.Length)
{
break;
}
}
msg("文件:你对" + socket.RemoteEndPoint.ToString() + " 发送文件" + strMsg);
}
}
接收文件代码: FileStream fs = null;
void Receive() //接收消息
{
try
{
while (isOnlie)
{
byte[] by = new byte[1024 * 1024 * 1];
int socLength = clientSocket.Receive(by);
if (by[0] == 0)
{
AppendLine("消息:服务端对你说 " + System.Text.Encoding.Default.GetString(by, 1, socLength - 1));
}
else if (by[0] == 1)
{
if (isSave)
{
fs.Write(by, 1, socLength - 1);
if (socLength < by.Length)
{
fs.Flush();
fs.Dispose();
isSave = false;
AppendLine("文件:你的文件地址为:" + FileName);
FileName = string.Empty;
break;
}
}
else
{
SaveFileDialog sd = new SaveFileDialog();
if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileName = sd.FileName;
fs = new FileStream(FileName, FileMode.OpenOrCreate);
fs.Write(by, 1, socLength - 1);
isSave = true;
if (socLength < by.Length)
{
fs.Flush();
fs.Dispose();
isSave = false;
AppendLine("文件:你的文件地址为:" + FileName);
FileName = string.Empty;
break;
}
}
}
}
}
}
catch (Exception ex)
{
btnLianJie.Text = "连接";
AppendLine("连接:服务端下线了");
clientSocket.Dispose();
}
}
接收端在接收到这个消息时候,执行创建文件操作,而不是你第一次接收到数据才弹出文件框选择目录创建文件。
接收端创建文件完毕后,向发送端返回可以传输文件的消息。
发送端开始传输文件,接收端开始接收文件。
当发送端发送完毕文件,发送一个发送完毕的指令,收端开始停止接收,关闭创建的文件。
如果发送端传输过程出现异常停止,try...Catch...,获取到异常,然后向接收端发送传输异常的消息,接收端接收到这个消息,执行停止接受文件和删除已创建文件操作。
-----------------------------------
总之,就是你收发双方协议有问题