public void WaitForSendData() { NetworkStream stream = _tcp.GetStream(); try { while (true) { try { byte[] data = new byte[1024]; int recLen = stream.Read(data, 0, 1024); if (recLen == 0) break; TCommand command = new TCommand(data, recLen); ExtractRecStr(command, stream); } catch (Exception) { break; } } } finally { stream.Close(); _tcp.Close(); } }
private void OnGetFile(TCommand command, NetworkStream stream) { #region 根据客户端已下载的字节数来定位文件流 long currentSize = Convert.ToInt64((string)command.argList[1]); long fileLength; long needLength; FileStream s = GetData.GetFileStream((string)command.argList[0]); if (s == null) return; fileLength = s.Length; if (currentSize >= fileLength) return; s.Position = currentSize;//如果已经下载了100字节,则position应该设置为100,即从100开始传输 #endregion try { #region 发送数据 while (true) { needLength = fileLength - s.Position; if (needLength == 0) break; if (needLength > 1024) needLength = 1024; byte[] data = new byte[1024]; int len = s.Read(data, 0, 1024); if (len == 0) break; stream.Write(data, 0, len); } #endregion } catch (Exception) { } finally { s.Close(); } }
private void ExtractRecStr(TCommand command, NetworkStream stream) { switch (command.commandStyle) { case CommandStyleEnum.cList: OnGetFileList(stream); break; case CommandStyleEnum.cGetFileLength: OnGetFileLength(command, stream); break; case CommandStyleEnum.cGetFile: OnGetFile(command, stream); break; default: break; } }
public static TCommand GetFileListCommand() { TSysConfig sysConfig = new TSysConfig(); string path = sysConfig.GetIniString("path", "-1"); if (path == "-1") { MessageBox.Show("没有从系统配置文件中找到目录"); return null; } string[] files = GetFileList(path); TCommand command = new TCommand(CommandStyleEnum.cListReturn); foreach (string s in files) command.AppendArg(s); return command; }
private void OnGetFileLength(TCommand command, NetworkStream stream) { long fileLength = GetData.GetFileLength((string)command.argList[0]); TCommand tempCommand = null; if (fileLength == 0) tempCommand = new TCommand(CommandStyleEnum.cGetFileLengthReturnNone); else tempCommand = new TCommand(CommandStyleEnum.cGetFileLengthReturn); tempCommand.AppendArg(fileLength.ToString()); byte[] data = tempCommand.ToBytes(); stream.Write(data, 0, data.Length); }
public void GetFile() { #region 定义变量 FileStream s; long currentSize; int p = 0; #endregion #region 获取当前下载进度 try { s = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); currentSize = s.Length; s.Position = currentSize; p = Convert.ToInt32(((double)currentSize / (double)_fileLength) * 100); dd a = delegate() { _p.Value = p; }; _p.Invoke(a); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } #endregion #region 判断文件是否已经下载完毕 if (currentSize == _fileLength) { MessageBox.Show("文件已经下载完毕!"); s.Close(); return; } #endregion #region 开始下载 NetworkStream stream = _tcp.GetStream(); try { TCommand command = new TCommand(CommandStyleEnum.cGetFile); command.AppendArg(_fileName); command.AppendArg(currentSize.ToString()); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); while (true) { byte[] recData = new byte[1024]; int recLen = stream.Read(recData, 0, 1024); if (recLen == 0)//断开 return; s.Write(recData, 0, recLen); currentSize += recLen; p = Convert.ToInt32(((double)currentSize / (double)_fileLength) * 100); if (p != _p.Value) { dd a = delegate() { _p.Value = p; Application.DoEvents(); }; _p.Invoke(a); } if (currentSize == _fileLength) { MessageBox.Show("下载完毕!"); return; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { #region 释放 stream.Close(); _tcp.Close(); s.Close(); #endregion } #endregion }
public void GetFileList() { if (_tcp == null) return; if (_listBox == null) return; NetworkStream stream = _tcp.GetStream(); try { TCommand command = new TCommand(CommandStyleEnum.cList); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); byte[] recData = new byte[9999]; int recLen = stream.Read(recData, 0, recData.Length); if (recLen == 0) return; command = new TCommand(recData, recLen); if (command.commandStyle != CommandStyleEnum.cListReturn) return; dd a = delegate() { _listBox.Items.Clear(); for (int i = 0; i < command.argList.Count; i++) { _listBox.Items.Add((string)command.argList[i]); } }; _listBox.Invoke(a); } finally { stream.Close(); _tcp.Close(); } }
private long GetFileLength(string fileName) { TcpClient tcp = null; NetworkStream stream = null; try { #region 建立连接,与发送请求文件长度的命令 tcp = new TcpClient(); tcp.Connect(_server, _port); stream = tcp.GetStream(); TCommand command = new TCommand(CommandStyleEnum.cGetFileLength); command.AppendArg(fileName); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); #endregion #region 接收数据 byte[] recData = new byte[1024]; int recLen = stream.Read(recData, 0, recData.Length); command = new TCommand(recData, recLen); #endregion #region 转换文件长度 if (command.commandStyle != CommandStyleEnum.cGetFileLengthReturn) return 0; if (command.argList.Count == 0) return 0; long fileLength = 0; try { fileLength = Convert.ToInt64((string)command.argList[0]); } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } #endregion return fileLength; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } finally { #region 释放 if (stream != null) stream.Close(); if (tcp != null) tcp.Close(); #endregion } }