using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; namespace Lskj.SocketClient { public partial class Form1 : Form { /// /// 每次上传数据包大小 /// public static int UploadPacketSize = 32 * 1024; /// /// 每次下载数据包大小 /// public static int DownLoadPacketSize = 64 * 1024; private ClientUploadSocket _uploadSocket; private ClientDownLoadSocket _downSocket; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { _uploadSocket = new ClientUploadSocket(); _uploadSocket.Connect("127.0.0.1", 9999); ShowMsg("Connect Server Success.127.0.0.1:9999"); _uploadSocket.DoActive(); _uploadSocket.DoLogin("admin", "admin"); ShowMsg("Login Server Success"); ShowMsg("Please Input Upload FileName"); } catch (Exception ex) { ShowMsg("Connect Server Fault." + ex.Message); } } private void button2_Click(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { for (int i = 0; i < 3; i++) //发送失败后,尝试3次重发 { if (SendFile(openFileDialog1.FileName)) { ShowMsg("Upload File Success"); break; } Thread.Sleep(3 * 1000); //发送失败等待3S后重连 } } } catch (Exception ex) { ShowMsg("SendFile Fault." + ex.StackTrace); } } private bool SendFile(string fileName) { FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite); try { try { long fileSize = fileStream.Length; if (!_uploadSocket.DoUpload("", Path.GetFileName(fileName), ref fileSize)) throw new Exception(_uploadSocket.ErrorString); fileStream.Position = fileSize; label1.Text = Path.GetFileName(fileName) + ":" + fileSize + "/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB"; byte[] readBuffer = new byte[UploadPacketSize]; long uploadSize = 0; while (fileStream.Position < fileStream.Length) { int count = fileStream.Read(readBuffer, 0, UploadPacketSize); if (!_uploadSocket.DoData(readBuffer, 0, count)) throw new Exception(_uploadSocket.ErrorString); uploadSize += count; Application.DoEvents(); label1.Text = Path.GetFileName(fileName) + ":" + Convert.ToInt32(uploadSize / 1024 / 1024) + "MB/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB"; ShowMsg("本地传输数据大小:" + uploadSize); } if (!_uploadSocket.DoEof(fileStream.Length)) throw new Exception(_uploadSocket.ErrorString); return true; } catch (Exception E) { ShowMsg("Upload File Error: " + E.Message); return false; } } finally { fileStream.Close(); } } public bool DownFile(string fileName) { FileStream fileStream = null; try { saveFileDialog1.FileName = fileName; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { long localSize = 0; long fileSize = 0; if (File.Exists(saveFileDialog1.FileName)) { fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite); localSize = fileStream.Length; } if (!_downSocket.DoDownLoad("", fileName, ref fileSize)) throw new Exception(_uploadSocket.ErrorString); ShowMsg(fileName + string.Format("({0}/{1}kb)", localSize, fileSize)); // 开始下载 byte[] readBuffer = new byte[DownLoadPacketSize]; long downloadSize = localSize; //while (fileStream.Position < fileStream.Length) //{ // fileStream.Write(readBuffer, 0, DownLoadPacketSize); // if (!_uploadSocket.DoData(readBuffer, 0, count)) // throw new Exception(_uploadSocket.ErrorString); // uploadSize += count; // Application.DoEvents(); // label1.Text = Path.GetFileName(fileName) + ":" + Convert.ToInt32(uploadSize / 1024 / 1024) + "MB/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB"; // ShowMsg("本地下载数据大小:" + uploadSize); //} //if (!_uploadSocket.DoEof(fileStream.Length)) // throw new Exception(_uploadSocket.ErrorString); } return true; } catch (Exception ex) { ShowMsg("DownLoadFile Fault." + ex.StackTrace); } finally { if (fileStream != null) fileStream.Close(); } return false; } public void ShowMsg(string msg) { this.Invoke((EventHandler)delegate { this.textBox1.Text += msg + "\r\n"; this.textBox1.ScrollToCaret(); }); } private void button3_Click(object sender, EventArgs e) { try { string fileName = "代码注释工具.rar"; for (int i = 0; i < 3; i++) //发送失败后,尝试3次重发 { if (DownFile(fileName)) { ShowMsg("DownLoad File Success"); break; } Thread.Sleep(3 * 1000); //发送失败等待3S后重连 } } catch (Exception ex) { ShowMsg("SendFile Fault." + ex.StackTrace); } } private void button4_Click(object sender, EventArgs e) { try { _downSocket = new ClientDownLoadSocket(); _downSocket.Connect("127.0.0.1", 9999); ShowMsg("Connect Server Success.127.0.0.1:9999"); _downSocket.DoActive(); _downSocket.DoLogin("admin", "admin"); ShowMsg("Login Server Success"); } catch (Exception ex) { ShowMsg(ex.Message); } } } }