Пример #1
0
        public void fn_testjson()
        {
            //test json

            MyJsonData t_jsondata = new MyJsonData("gong", 30);


            string t_myjson = JsonMapper.ToJson(t_jsondata);

            Console.WriteLine("json=" + t_myjson);

            MyJsonData t_fromJson = null;

            try
            {
                t_fromJson = JsonMapper.ToObject <MyJsonData>(t_myjson);
            }
            catch (Exception _e)
            {
                Console.WriteLine(_e.ToString());
            }


            if (t_fromJson != null)
            {
                Console.WriteLine("--->" + t_fromJson.m_name + ",age=" + t_fromJson.m_age);
            }
        }
Пример #2
0
        public void fn_startClient()
        {
            IPAddress t_ip = IPAddress.Parse("10.15.19.84");

            m_ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                Console.WriteLine("链接中...");
                m_ClientSocket.Connect(new IPEndPoint(t_ip, 8885));
                //创建接收线程
                Task t_handle = new Task(() => {
                    if (m_ClientSocket.IsBound)
                    {
                        fnp_received(m_ClientSocket);
                    }
                    else
                    {
                        Task t_connect = new Task(fnpp_connectAgain);
                        t_connect.Wait(1000);
                        t_connect.Start();
                    }
                });
                t_handle.Start();

                Console.WriteLine("链接成功");
            }
            catch (Exception _e)
            {
                Task t_connect = new Task(fnpp_connectAgain);
                t_connect.Wait(1000);
                t_connect.Start();
                Console.WriteLine(_e.Message);
                return;
            }
            finally {
            }

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    Thread.Sleep(1000);
                    //string t_sendmsg = "to server " + DateTime.Now;
                    string     t_sendmsg;
                    MyJsonData t_data = new MyJsonData(" xxx- ", i);
                    t_sendmsg = JsonMapper.ToJson(t_data);

                    m_ClientSocket.Send(Encoding.ASCII.GetBytes(t_sendmsg));
                    Console.WriteLine("发给服务器:" + t_data.m_name);
                }
                catch (Exception _e) {
                    Console.WriteLine(_e.Message);
                }
            }
        }