问题描述
服务端:namespaceControl_Client{publicpartialclassMain_Form:Form{#region全局变量部分TcpClientClient;TcpListenerLis;NetworkStreamStream;Socketsocket;SocketLis_socket;StringLocalDisk_List="$GetDir||";StringOnline_Order="$Online||";StringProcess_List="$GetProcess||";UdpClientUDP_Client=newUdpClient();publicdelegatevoidPt();#endregion#region命令处理函数publicvoidOrder_Catcher(String[]Order_Set){switch(Order_Set[0]){case"$Return":switch(Order_Set[1]){case"#Online_OK":this.Online_OK();break;}break;case"$GetProcess":this.Get_Process();break;case"$KillProcess":this.Kill_Process(Order_Set[1]);break;try{using(NetworkStreamNs=newNetworkStream(this.Lis_socket)){Ns.Write(Encoding.Default.GetBytes("$ActiveHDC||True"),0,Encoding.Default.GetBytes("$ActiveHDC||True").Length);Ns.Flush();}this.UDP_Client.Connect(Globle.Host,Globle.UDP_Port);if(this.UDP_Client.Client.Connected){}}catch(Exceptionex){MessageBox.Show("尝试发送激活HDC信息失败:"+ex.Message);}break;case"$ShutDown":DoExitWin(1);break;case"$LogOut"://MessageBox.Show("Logout");DoExitWin(0);break;}}主控端:namespace主控端{publicpartialclassMain_Form:Form{publicdelegatevoidPt();TcpListenerLis;Socketsocket;Type_Clienttc=null;StringRemove_Ip="";publicMain_Form(){InitializeComponent();}publicvoidListen_Port(){Lis=newTcpListener(IPAddress.Any,Globle.Port);this.Sys_Icon.ShowBalloonTip(5000,"端口监听成功","端口["+Globle.Port+"]监听成功!r等待主机上线......",ToolTipIcon.Info);while(Globle._IsListen_Port){try{Lis.Start();this.Program_State.Text="["+Globle.Port+"]"+"端口监听成功...";this.BeginInvoke(newPt(this.Change_Label_Color_Green));}catch(Exceptionex){this.Program_State.Text="监听端口"+Globle.Port+"失败....";this.BeginInvoke(newPt(this.Change_Label_Color_Red));}try{this.socket=Lis.AcceptSocket();while(Globle._IsResvice_Message){using(NetworkStreamns=newNetworkStream(this.socket)){try{byte[]bb=newbyte[1024];intRes_Len=ns.Read(bb,0,bb.Length);String[]Order_Set=Encoding.Default.GetString(bb,0,Res_Len).Split(newString[]{"||"},StringSplitOptions.RemoveEmptyEntries);this.Order_Catcher(Order_Set);}catch(Exceptionex){};}}}catch(Exceptionex){//MessageBox.Show("监听成功后出现错误:"+ex.Message);}}}privatevoidMain_Form_Load(objectsender,EventArgse){Threadthread=newThread(newThreadStart(this.Listen_Port));thread.Start();}publicvoidOrder_Catcher(String[]Order_Set){switch(Order_Set[0]){case"$Online":this.Online(Order_Set);break;case"$OffLine":this.OffLine(Order_Set[1]);break;}}publicvoidOnline(String[]Order_Set){Type_Clienttc=newType_Client();tc.Ip=((IPEndPoint)this.socket.RemoteEndPoint).Address.ToString();tc.Software=Order_Set[1];tc.Computer_Name=Order_Set[2];tc.Customer=Order_Set[3];tc.System_Info=Order_Set[4];tc.Cpu=Order_Set[5];tc.Memory=Order_Set[6];tc.Socket=this.socket;if(tc.Software=="专用版本"){this.tc=tc;this.BeginInvoke(newPt(this.reFlush_OnlineList));}using(NetworkStreamNs=newNetworkStream(tc.Socket)){Ns.Write(Encoding.Default.GetBytes("$Return||#Online_OK"),0,Encoding.Default.GetBytes("$Return||#Online_OK").Length);Ns.Flush();}Globle.Online_Number++;}publicvoidOffLine(StringIp){for(inti=0;i<Globle.Online_Computer_Attr.Count;i++){if(Globle.Online_Computer_Attr[i].Ip==Ip){Globle.Online_Computer_Attr.Remove(Globle.Online_Computer_Attr[i]);break;}}this.Remove_Ip=Ip;this.BeginInvoke(newPt(this.Remove_ListView_Computer));}publicvoidRemove_ListView_Computer(){for(inti=0;i<this.Nomarl_Member.Items.Count;i++){if(this.Nomarl_Member.Items[i].Text.Trim()==this.Remove_Ip.Trim()){this.Nomarl_Member.Items[i].Remove();break;}}Globle.Online_Number--;this.Customer_Online_Info.Items.Add(this.Remove_Ip,"主机:"+this.Remove_Ip+"下线成功!",0);this.OnLine_Counter.Text=""+(int.Parse(this.OnLine_Counter.Text)-1);}privatevoidMain_Form_FormClosing(objectsender,FormClosingEventArgse){Environment.Exit(0);}publicvoidChange_Label_Color_Red(){this.Program_State.ForeColor=Color.Red;}publicvoidChange_Label_Color_Green(){this.Program_State.ForeColor=Color.Green;}publicvoidreFlush_OnlineList(){boolFlag=false;for(inti=0;i<Globle.Online_Computer_Attr.Count;i++){if(Globle.Online_Computer_Attr[i].Ip==tc.Ip){Flag=true;break;}}if(!Flag){Globle.Online_Computer_Attr.Add(tc);this.Nomarl_Member.Items.Add(this.tc.Ip,this.tc.Ip,0);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.Software);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.Computer_Name);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.Customer);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.System_Info);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.Cpu);this.Nomarl_Member.Items[this.tc.Ip].SubItems.Add(this.tc.Memory);this.Customer_Online_Info.Items.Add(this.tc.Ip,"主机:"+this.tc.Ip+"上线了!",1);if(this.tc.Customer!=""){this.Customer_Online_Info.Items[this.tc.Ip].SubItems.Add(this.tc.Customer);}else{this.Customer_Online_Info.Items[this.tc.Ip].SubItems.Add("[没有备注]");}this.Sys_Icon.ShowBalloonTip(5000,"有主机上线啦!","主机:"+tc.Ip+"上线啦",ToolTipIcon.Info);this.OnLine_Counter.Text=Globle.Online_Number.ToString();}}privatevoidbutton5_Click(objectsender,EventArgse){if(this.Nomarl_Member.SelectedItems.Count>0){StringIp=this.Nomarl_Member.SelectedItems[0].Text.Trim();for(inti=0;i<Globle.Online_Computer_Attr.Count;i++){if(Globle.Online_Computer_Attr[i].Ip==Ip){Process_ManagerPM=newProcess_Manager(Ip,this);PM.Show();}}}}
解决方案
本帖最后由 oyljerry 于 2011-12-31 17:38:24 编辑
解决方案二:
需要耐心看!
解决方案三:
看完了给我解释一下就更好了