Example #1
0
        public void SendServerMessage(int cmd, string message)
        {
            CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE();

            rep.Cmd     = cmd;
            rep.Message = message;

            MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep);

            byte[] entityData = ms.ToArray();

            byte[] messagedate = MessageCoder.MessageEncoding(entityData);

            _socket.Send(messagedate, messagedate.Length, 0);
        }
Example #2
0
        public void SendTestMessage()
        {
            CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE();

            rep.Cmd     = 11;
            rep.Message = "bsadsdfasf";

            MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep);

            byte[] entityData = ms.ToArray();

            byte[] messagedate = MessageCoder.MessageEncoding(entityData);

            _socket.Send(messagedate, messagedate.Length, 0);
        }
Example #3
0
        static void CreatOneConnect()
        {
            try
            {
                int    port = 2012;
                string host = "127.0.0.1";
                //string host = "134.175.17.67";

                IPAddress  ip  = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);                                                    //把ip和端口转化为IPEndPoint实例
                Socket     c   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket
                Console.WriteLine("Conneting...");
                c.Connect(ipe);                                                                               //连接到服务器


                CMD_BASE_MESSAGE rep = new CMD_BASE_MESSAGE();
                rep.Cmd     = 0x10000001;
                rep.Message = "bsadsdfasf";


                MemoryStream ms = MessageTransformation.Serialize <CMD_BASE_MESSAGE>(rep);

                byte[] entityData = ms.ToArray();

                byte[] messagedate = MessageCoder.MessageEncoding(entityData);

                c.Send(messagedate, messagedate.Length, 0);
                c.Send(messagedate, messagedate.Length, 0);
                c.Send(messagedate, messagedate.Length, 0);
                c.Send(messagedate, messagedate.Length, 0);
                c.Send(messagedate, messagedate.Length, 0);
                c.Send(messagedate, messagedate.Length, 0);

                //Console.Write("断开连接");
                c.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
        }