Ejemplo n.º 1
0
 /**
  * 发送消息 - MsgData
  */
 public void sendMsgData(MsgData md, List <Socket> sendList)
 {
     foreach (Socket so in sendList)
     {
         sendMsgData(md, so);
     }
 }
Ejemplo n.º 2
0
        /*************************************************************************** Socket 发送消息 **********/


        /**
         * 接受信息
         */
        public void RecieveMsg(Object obj)
        {
            Socket aSocket = (Socket)obj;

            while (true)
            {
                try {
                    NetworkStream ns        = new NetworkStream(aSocket);
                    StreamReader  sr        = new StreamReader(ns);
                    string        resultStr = sr.ReadLine();

                    MsgData md  = MsgData.DeserializeMsg(resultStr);
                    String  msg = md.msg;

                    //this.preventAlwaysLoop();
                    testcount++;
                    if (testcount > 100)
                    {
                        testcount = 0;
                        throw new Exception("循环次数太多!");
                    }

                    appendToHistory("Msg -来自" + aSocket.RemoteEndPoint.ToString() + "\n" + msg + "\n");
                }
                catch (Exception ex) {
                    appendToHistory("异常:" + ex.Message + "\n");
                    this.closeTheSocket(aSocket);

                    cliList_live = this.getConnctedCli();
                    this.showCliList(cliList_live);
                    this.sendOnlineListToCli(cliList_live);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        /**
         * 序列化为Jison
         */
        public static String SerializeMsg(MsgData md)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            string mdString           = json.Serialize(md);

            return(mdString);
        }
Ejemplo n.º 4
0
        /**
         * 反序列数据
         */
        public static MsgData DeserializeMsg(String mdString)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            MsgData md = json.Deserialize <MsgData>(mdString);

            return(md);
        }
Ejemplo n.º 5
0
        /*************************************************************************** END Socket 发送消息 **********/


        /*************************************************************************** Socket 发送消息 **********/
        //////只有服务的发送, 客户端的接收才用 MsgData 对象的数据格式
        ////// 其他时候, 客户端-》服务端, 客户端《-》客户端都直接发送字符串

        /**
         * 发送消息 - MsgData
         */
        public void sendMsgData(MsgData md, Socket so)
        {
            String mdString = MsgData.SerializeMsg(md);

            NetworkStream ns = new NetworkStream(so);
            StreamWriter  sw = new StreamWriter(ns);

            sw.WriteLine(mdString);
            sw.Flush();
        }
Ejemplo n.º 6
0
        /**
         * 发送在线客户端地址给,所有在线客户端
         */
        public void sendOnlineListToCli(List <Socket> cliList)
        {
            String cliString = "";

            foreach (Socket so in cliList)
            {
                cliString = cliString + so.RemoteEndPoint.ToString() + "&";
            }
            if (cliString.Length > 0)
            {
                cliString = cliString.Substring(0, cliString.Length - 1);
            }
            MsgData md = new MsgData("", cliString);

            this.sendMsgData(md, cliList);
        }
Ejemplo n.º 7
0
        /**
         * 监听客户端
         */
        public void listenClientConn()
        {
            while (true)
            {
                Socket aSocket = serverS.Accept();

                clientList.Add(aSocket);
                cliList_live = this.getConnctedCli();
                this.showCliList(cliList_live);
                this.sendOnlineListToCli(cliList_live);

                appendToHistory("连接来自" + aSocket.RemoteEndPoint.ToString() + "\n");

                MsgData md = new MsgData("这里是服务端");
                this.sendMsgData(md, aSocket);

                Thread newThread = new Thread(RecieveMsg);
                newThread.Name         = "receieveeeee";
                newThread.IsBackground = true;
                newThread.Start(aSocket);
            }
        }
Ejemplo n.º 8
0
        /*************************************************************************** END Socket 发送消息 **********/


        /**************************************************************************** 按钮 *************/

        private void btn_send_Click(object sender, EventArgs e)
        {
            String        msg      = getMsgContent();
            List <Socket> sendList = this.getSendTarget();

            if (sendList.Count == 0)
            {
                appendToHistory("请选择发送对象\n");
                return;
            }
            MsgData md = new MsgData(msg);

            this.sendMsgData(md, sendList);

            //显示发送信息
            appendToHistory("服务端 - 发往");
            foreach (Socket so in sendList)
            {
                appendToHistory(so.RemoteEndPoint.ToString() + ", ");
            }
            appendToHistory("\n" + msg + "\n");
            tb_msg.Clear();
        }