编写简易QQ的过程中出现的有关文件的问题
大家好,第一次发帖,请多关照。文本对话已经实现了,我贴一下文件传输部分的代码。
这次真的是修了很多次代码但是不知道问题在哪里,希望能够请大家指点一下迷津。谢谢。
现在的问题是一传送文件就会出现错误,信息是:“System.ArgumentException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理。
虽然他这么说了但是还是不太明白意思,所以求助一下大家。
这是发送文件时候的代码
程序代码:
if (fileInSending == false) { fileInSending = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo file = new FileInfo(openFileDialog1.FileName); using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read)) { BinaryWriter BWriter = new BinaryWriter(NetStream); BWriter.Write(1);//代号1表示单个文件的总长度+文件名字节长度+文件名 BWriter.Write(fs.Length);//总长 byte[] nameBuffer = Encoding.UTF8.GetBytes(Path.GetFileName(file.FullName)); BWriter.Write(nameBuffer.Length);//文件名长度 NetStream.Write(nameBuffer, 0, nameBuffer.Length);//文件名 int len = 0; byte[] buffer = new byte[4000]; while (NetStream.CanWrite) { len = fs.Read(buffer, 0, buffer.Length); BWriter.Write(2);//代号2表示文件块的长度 BWriter.Write(len); BWriter.Write(4);//代号4表示文件 NetStream.Write(buffer, 0, len); buffer = new byte[4000]; } fileInSending = false; } } }
这是文件接收时的代码,里面用到的未申明变量都是类变量,backgroundWorker2组件是在计时器计时的时候触发的。因为把服务端和客户端都写在了一起,是通过界面上的一些选择来控制作为服务端还是客户端的,所以代码中有两个Client,Client是客户端,Subclient是服务端。命名好像确实有点奇怪了,请大家包涵。
程序代码:
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { if (Client is TcpClient) { if (Client.Available != 0) { BinaryReader BReader = new BinaryReader(NetStream); codeNum = BReader.ReadInt32(); } } else if (Subclient is TcpClient) { if (Subclient.Available != 0) { BinaryReader BReader = new BinaryReader(NetStream); codeNum = BReader.ReadInt32(); } } } private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { BinaryReader BReader = new BinaryReader(NetStream); #region 文本长度获取+文本读取 if (codeNum == 0)//代号0表示文本长度 { textLength = BReader.ReadInt32(); } else if (codeNum == 3)//代号3表示文本 { byte[] buffer = new byte[textLength]; NetStream.Read(buffer, 0, buffer.Length); string text = ""; text = Encoding.UTF8.GetString(buffer); textLength = 0; if (text != null) { ShowMessage(text, false); } } #endregion #region 文件总长度获取+文件名获取+每个文件块大小获取+文件获取 else if (codeNum == 1)//代号1表示单个文件的总长度+文件名字节长度+文件名 { fileFullLength = BReader.ReadInt64(); fileNameLength = BReader.ReadInt32(); byte[] buffer = new byte[fileNameLength]; fileName = Encoding.UTF8.GetString(buffer); fileRecievedLength = 0; FileDeliver fd = new FileDeliver(); fd.fileRecievedPercent = (int)Math.Floor((double)(fileRecievedLength / fileFullLength)) * 100; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { saveFileDialog1.FileName = fileName; fs = File.Create(saveFileDialog1.FileName); } } else if (codeNum == 2) { fileBlockLength = BReader.ReadInt32(); } else if (codeNum == 4) { byte[] buffer = new byte[fileBlockLength]; NetStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); fileRecievedLength = fs.Length; //判定是否文件接收完毕 if (fs.Length == fileFullLength) { fileFullLength = 0; fileNameLength = 0; fileName = ""; fileBlockLength = 0; fileRecievedLength = 0; } } #endregion }