예제 #1
0
        public CClientMsg(CSLib.Network.CTcpClient tcpClient)
        {
            m_tcpClient = tcpClient;

            // 此处使用简单的消息分发机制来演示
            m_msgFactory.AddMsgObject((UInt16)(Message.EMsgType.EMT_SAMPLE), (UInt16)Message.EMsgID.EMID_SAMPLE_NTF_OK, new Message.CMsgSampleNtfOk());
            m_msgFactory.AddMsgObject((UInt16)(Message.EMsgType.EMT_SAMPLE), (UInt16)Message.EMsgID.EMID_SAMPLE_REQ_SAY, new Message.CMsgSampleReqSay());
            m_msgFactory.AddMsgObject((UInt16)(Message.EMsgType.EMT_SAMPLE), (UInt16)Message.EMsgID.EMID_SAMPLE_RES_SAY, new Message.CMsgSampleResSay());
            m_msgExecute.MsgFactory = m_msgFactory;

            // 此处使用简单的消息处理机制来演示
            m_msgExecute.AddMsgExecFunc((UInt16)(Message.EMsgType.EMT_SAMPLE), (UInt16)Message.EMsgID.EMID_SAMPLE_RES_SAY, _OnMsgTestResSay);
        }
예제 #2
0
        static void Main(string[] args)
        {
            CSLib.Network.CTcpClient tcpClient = new CSLib.Network.CTcpClient();

            tcpClient.CbParsMsg      = CClientMsg.ParseMsg;
            tcpClient.CbTerminateMsg = CClientMsg.TerminateMsg;

            if (!tcpClient.Connect("127.0.0.1", 9000))
            {
                Console.WriteLine("连接服务器失败");
                return;
            }

            CSLib.Utility.CStream stream = new CSLib.Utility.CStream();

            Console.WriteLine(tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString());

            uint i = 1;

            while (true)
            {
                string data = "Client No.[";
                data += i.ToString();
                data += "]";

                stream.Write(data);

                i++;

                Console.WriteLine(data.ToString() + "{" + tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString() + "}");

                if (!tcpClient.SendAsync(stream))
                {
                    break;
                }

                stream.Reset();

                System.Threading.Thread.Sleep(1000);
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            CSLib.Network.CTcpClient tcpClient = new CSLib.Network.CTcpClient();

            CClientMsg clientMsg = new CClientMsg(tcpClient);

            tcpClient.CbParsMsg      = clientMsg.ParseMsg;
            tcpClient.CbTerminateMsg = CClientMsg.TerminateMsg;

            if (!tcpClient.Connect("127.0.0.1", 9500))
            {
                Console.WriteLine("连接服务器失败");
                return;
            }
            Message.CMsgSampleNtfOk ntfOk = new Message.CMsgSampleNtfOk();

            CSLib.Utility.CStream stream = new CSLib.Utility.CStream();
            ntfOk.Serialize(stream);
            if (!tcpClient.SendAsync(stream))
            {
                return;
            }

            stream.Reset();
            Console.WriteLine(tcpClient.LocalIPEndPoint.ToString() + "--->" + tcpClient.PeerIPEndPoint.ToString());

            uint i = 1;

            //DateTime dt = new DateTime(2013,3,4);
            // decimal dec = 12.12M;
            while (true)
            {
                stream.Reset();

                Message.CMsgSampleReqSay reqSay = new Message.CMsgSampleReqSay();

                reqSay.m_string  = "Client No.[";
                reqSay.m_string += i.ToString();
                reqSay.m_string += "]";
                byte[] vv;
                vv              = new byte[5];
                vv[1]           = 1;
                vv[2]           = 2;
                vv[3]           = 3;
                vv[4]           = 4;
                vv[0]           = 5;
                reqSay.m_bool   = true;
                reqSay.m_byte   = (byte)i;
                reqSay.m_bytes  = vv;
                reqSay.m_double = 1213.43435;
                reqSay.m_float  = 123.234f;
                reqSay.m_int    = 34534534;
                reqSay.m_long   = 3245675756675L;
                reqSay.m_short  = 32000;
                reqSay.m_uint   = 2100000000;
                reqSay.m_ulong  = 94242424242342L;
                reqSay.m_ushort = 65000;
                reqSay.Serialize(stream);

                i++;

                Console.WriteLine(reqSay.m_string + reqSay.m_bool + "  " + reqSay.m_byte + "  " + reqSay.m_bytes[0] + "  " + reqSay.m_bytes[1] + "  " + reqSay.m_bytes[2] + "  " + reqSay.m_bytes[3] + "  " + reqSay.m_bytes[4] + "  " + reqSay.m_double + "  " + reqSay.m_float + "  " + reqSay.m_int + "  " + reqSay.m_long + "  " + reqSay.m_short + "  " + reqSay.m_uint + "  " + reqSay.m_ulong + "  " + reqSay.m_ushort);

                if (!tcpClient.SendAsync(stream))
                {
                    break;
                }

                stream.Reset();

                System.Threading.Thread.Sleep(1000);
            }
        }