示例#1
0
        static void Main(string[] args)
        {
            Client client = new Client();

            client.Connect("127.0.0.1", 6666);
            Userinfo userinfo = new Userinfo();

            Console.WriteLine("名字:");
            userinfo.Name = Console.ReadLine();
            Console.WriteLine("密码:");
            userinfo.Pwd = Console.ReadLine();
            Protocaltest protocaltest = new Protocaltest();

            protocaltest.model  = 1;
            protocaltest.operat = 1;
            protocaltest.data   = userinfo;
            client.SendMessage(protocaltest);
            //while (str!="Quit")
            //{
            //    client.SendMessage(str);
            //    str = Console.ReadLine();

            //}
            Console.ReadKey();
        }
示例#2
0
        public void SendMessage(Protocaltest protocaltest)
        {
            MemoryStream    stream    = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, protocaltest);
            byte[] msg = stream.GetBuffer();
            socket.Send(msg);

            //socket.Send(Encoding.UTF8.GetBytes(msg));
        }
示例#3
0
 public void ClientSendMessage(Protocaltest protocaltest)
 {
 }
示例#4
0
        private void Recevie(object obj)
        {
            Socket client = obj as Socket;

            IPEndPoint point = client.RemoteEndPoint as IPEndPoint;

            try
            {
                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024];
                    int    msglen = client.Receive(buffer);

                    MemoryStream    memoryStream    = new MemoryStream(buffer, 0, msglen);
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    Protocaltest    protocaltest    = binaryFormatter.Deserialize(memoryStream) as Protocaltest;

                    switch (protocaltest.model)
                    {
                    case 1:    //用户
                        switch (protocaltest.operat)
                        {
                        case 1:        //注册
                            Userinfo userinfo = protocaltest.data as Userinfo;
                            string   sql      = "insert into user(name,password) values(@name,@pwd)";
                            int      result   = MysqlHelper.Insert(sql, new MySqlParameter("@name", userinfo.Name),
                                                                   new MySqlParameter("@pwd", userinfo.Pwd));
                            if (result == 1)
                            {
                                client.Send(Encoding.UTF8.GetBytes("成功"));
                                //BroadCast("广播", client);
                            }
                            break;

                        case 2:        //登陆
                            break;

                        default:
                            break;
                        }
                        break;

                    case 2:
                        break;

                    default:
                        break;
                    }

                    //string msg = Encoding.UTF8.GetString(buffer, 0, msglen);
                    //string str = point.Address + "[" + point.Port + "]:" + msg;
                    //Console.WriteLine(str);

                    //client.Send(Encoding.UTF8.GetBytes(msg + "OJBK"));
                    //BroadCast("广播", client);
                }
            }
            catch (Exception)
            {
                Console.WriteLine(point.Address + "[" + point.Port + "]:" + "已断开连接");
                AllClient.Remove(client);
            }
        }