using System;using System.Collections;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.IO;using System.Text;namespace Common...{ public class MessageClient : System.ComponentModel.Component ...{ private string server="10.177.203.106"; private string bkserver="10.177.203.100"; private System.Windows.Forms.StatusBarPanel m_sbar=null; private ArrayList msglist=new ArrayList(); private Byte[] read = new Byte[255]; private Stream s; private TcpClient tcpc; private string m_staff="620500.Admin.Admin"; Properties#region Properties public string Staff ...{ set...{m_staff=value;} } public string MainServer ...{ get...{return server;} set...{server=value;} } public string BackupServer ...{ get...{return bkserver;} set...{bkserver=value;} } public System.Windows.Forms.StatusBarPanel StatusBarPanel ...{ get...{return m_sbar;} set...{m_sbar=value;} } #endregion Methods#region Methods public void Start() ...{ tcpc = new TcpClient(); try ...{ try ...{ IPAddress ip = IPAddress.Parse(server); tcpc.Connect(ip, 10001); } catch(FormatException fe) ...{ tcpc.Connect(server, 10001); } } catch ...{ try ...{ try ...{ IPAddress ip = IPAddress.Parse(bkserver); tcpc.Connect(ip, 10001); } catch(FormatException fe) ...{ tcpc.Connect(bkserver, 10001); } } catch(Exception ex) ...{ Common.DingleErrorLog.Write(ex,"MainError.log","连接到服务器失败"); return; } } try ...{ s = tcpc.GetStream(); } catch (InvalidOperationException ex) ...{ Common.DingleErrorLog.Write(ex,"MainError.log","连接到服务器失败"); return; } Byte[] byteDateLine = Encoding.Unicode.GetBytes("00"+m_staff); s.Write(byteDateLine,0,byteDateLine.Length); AsyncCallback streamReceiverCallback = new AsyncCallback(ReceiverMessage); s.BeginRead(read,0, 255,streamReceiverCallback, null); } public void ReceiverMessage(IAsyncResult ar) ...{ int BytesRead=0; string strMsg=""; string strCmd=""; try ...{ try ...{ lock (s) ...{ BytesRead = s.EndRead(ar); } string r_msg = Encoding.Unicode.GetString(read, 0, BytesRead); strCmd=r_msg.Substring(0,2); strMsg=r_msg.Substring(2); DealMessage(strCmd,strMsg); } catch(Exception ex) ...{ DingleErrorLog.Write(ex,"MainError.log","服务器中断"); } lock(s) ...{ AsyncCallback streamReceiverCallback = new AsyncCallback(ReceiverMessage); s.BeginRead(read,0, 255,streamReceiverCallback, null); } } catch(Exception ex) ...{ DingleErrorLog.Write(ex,"MainError.log","服务器中断"); } } public void Close() ...{ if(s!=null) s.Close(); if(tcpc!=null) tcpc.Close(); } #endregion DealMessage#region DealMessage private void DealMessage(string cmd,string msg) ...{ switch(cmd) ...{ case "00"://停应用服务器 this.m_sbar.Text=msg; this.m_sbar.ToolTipText=msg; System.Windows.Forms.MessageBox.Show(msg,"停服务器通知",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); break; case "01"://程序更新 this.m_sbar.Text=msg; this.m_sbar.ToolTipText=msg; break; case "02"://一般消息 this.m_sbar.Text=msg; break; case "03"://紧急通知 this.m_sbar.Text=msg; System.Windows.Forms.MessageBox.Show(msg,"紧急通知",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); break; case "04"://允许私人交谈 this.m_sbar.Text=msg; break; case "05"://取消私人交谈 this.m_sbar.Text=msg; break; case "06"://Login Ok this.SendMessage("02"+this.m_staff); break; } } public void SendMessage(string msg) ...{ try ...{ Byte[] byteDateLine = Encoding.Unicode.GetBytes(msg); s.Write(byteDateLine,0,byteDateLine.Length); } catch(Exception ex) ...{ DingleErrorLog.Write(ex,"MainError.log","客户端发送消息失败:"+msg); } } public void LoginOK() ...{ this.SendMessage("00"+this.m_staff); } public void SignOut() ...{ this.SendMessage("01"+this.m_staff); } public void Interrupt() ...{ this.SendMessage("03"+this.m_staff); } #endregion MessageClient#region MessageClient private System.ComponentModel.Container components = null; public MessageClient(System.ComponentModel.IContainer container) ...{ container.Add(this); InitializeComponent(); } public MessageClient() ...{ InitializeComponent(); } Component Designer generated code#region Component Designer generated code private void InitializeComponent() ...{ components = new System.ComponentModel.Container(); } #endregion #endregion }}