/*——绘制进度条——*/ private void Progress() { /*--- SEEK命令 ---*/ // 计时开始 stpwth.Start(); while (current_progress != total_progress) { socket5656.Send(Encoding.UTF8.GetBytes("SEEK")); socket5656.Receive(buffer5656); current_progress = ConcUtilities.GetLong(buffer5656); double prog_in_100 = (double)current_progress / total_progress * 100; updateProgressBar((long)prog_in_100); sizeStatus.Text = String.Format("{0:F}%", prog_in_100); Thread.Sleep(100);//当前任务休眠0.1秒 } }
/*——与端口5656进行通信——*/ private bool EstablishConnect5656() { socket5656 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建负责与5656端口进行通信的socket对象 IPAddress ip = IPAddress.Parse("127.0.0.1"); //获取服务器IP地址 IPEndPoint point5656 = new IPEndPoint(ip, concMonitor); try { socket5656.Connect(point5656);//连接服务器IP与端口 txtDialogue.AppendText("Conc was opened.\r\n"); } catch (Exception) { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } /*--- CLNT命令 ---*/ socket5656.Send(Encoding.UTF8.GetBytes("CLNT"));//Encoding.UTF8.GetBytes()将要发送的字符串转换成UTF8字节数组 if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } /*--- RMAD命令 ---*/ socket5656.Send(Encoding.UTF8.GetBytes("RMAD")); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } socket5656.Send(ConcUtilities.ConvertHex(serverAddr.Length));//目标主机名长度 if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } socket5656.Send(Encoding.UTF8.GetBytes(serverAddr));//目标主机名 if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } /*--- RMPO命令 ---*/ socket5656.Send(Encoding.UTF8.GetBytes("RMPO")); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } //目标主机端口长度 socket5656.Send(ConcUtilities.ConvertHex(port.Length)); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } socket5656.Send(Encoding.UTF8.GetBytes(port));//目标主机端口 if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } /*--- CONN命令 ---*/ socket5656.Send(Encoding.UTF8.GetBytes("CONN")); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } /*--- SEND命令 ---*/ socket5656.Send(Encoding.UTF8.GetBytes("SEND")); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } socket5656.Send(ConcUtilities.ConvertHex(Encoding.UTF8.GetByteCount(filePath))); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } socket5656.Send(Encoding.UTF8.GetBytes(filePath)); if (receive_Message(socket5656, buffer5656) != "OK") { txtDialogue.AppendText("Monitor port on conc was not connectable\r\n"); return(false); } return(true); }
/*——有关9898端口全局函数定义部分:——*/ void Communicate9898() { while (true) //一直重复 /* socket9898.执行 * {Shutdown(SocketShutdown.Both); * socket9898.Close();*/ { int r = 0; while ((r = socket9898.Receive(buffer9898)) == 0) { ; } socket9898.Send(Encoding.UTF8.GetBytes("OK")); string s = Encoding.UTF8.GetString(buffer9898, 0, r); string info = s.Substring(0, 2); if (info == "ER") { string error_info = s.Substring(3, 3); if (error_info == "001") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "Could not start sending process, because the remote server address at the given port is not connectable. Errorcode: " + error_info, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (error_info == "002") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "File or directory could not be found: ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (error_info == "003") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "Connection failed while transferring. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (error_info == "005") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "Client/Server status fault. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (error_info == "006") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "Instruction was unable to send.", "ERROR 006", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (error_info == "007") { SystemSounds.Exclamation.Play(); MessageBox.Show(this, "Bad list.send.", "ERROR 007", MessageBoxButtons.OK, MessageBoxIcon.Error); } try { TaskCompleted(); } catch (Exception) { }; } else if (info == "CP") { // 清洁函数 try { TaskCompleted(); } catch (Exception) { }; } else if (info == "IF") { string tip_info = s.Substring(3, 3); //extBox_Dialog.AppendText("收到了IF" + tip_info); if (tip_info == "001") { txtDialogue.AppendText("Connection to server was established.\r\n"); } else if (tip_info == "002") { /*--- TSZE命令 ---*/ txtDialogue.AppendText("File transfer has just started.\r\n"); socket5656.Send(Encoding.UTF8.GetBytes("TSZE")); socket5656.Receive(buffer5656); //获取总共任务量 total_progress = ConcUtilities.GetLong(buffer5656); txtDialogue.AppendText("Task size: " + total_progress + "\r\n"); // 设置进度条 EstablishProgress(); // test // txtDialogue.AppendText("Progress bar set done\r\n"); } else if (tip_info == "003") { txtDialogue.AppendText("Transfer is over.\r\n"); stpwth.Stop(); TimeSpan ts = stpwth.Elapsed; this.txtDialogue.AppendText(String.Format("Speed: {0:F} MB/s", ((double)total_progress / 1048576) / ts.TotalSeconds)); stpwth.Reset(); // 设置completed图像 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormMain)); this.imgConc.Image = (System.Drawing.Image)(resources.GetObject("imgCompleted.Image")); try { TaskCompleted(); } catch (Exception) { }; return; } else { } } else if (info == "QU") { // socket9898.Send(Encoding.UTF8.GetBytes("OK")); int r1 = socket9898.Receive(buffer9898); string s1 = Encoding.UTF8.GetString(buffer9898, 0, r1); socket9898.Send(Encoding.UTF8.GetBytes("OK")); int r2 = socket9898.Receive(buffer9898); string s2 = Encoding.UTF8.GetString(buffer9898, 0, r2); DialogResult result = MessageBox.Show(s2, "Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { socket9898.Send(Encoding.UTF8.GetBytes("OK")); return; } else { socket9898.Send(Encoding.UTF8.GetBytes("NO")); return; } } //txtDialogue.AppendText(socketClient.RemoteEndPoint + ":" + s + "\r\r\n"); } }