Beispiel #1
0
        ServerObject server; // объект сервера

        public ClientObject(TcpClient tcpClient, ServerObject serverObject)
        {
            Id     = Guid.NewGuid().ToString();
            client = tcpClient;
            server = serverObject;
            serverObject.AddConnection(this);
        }
Beispiel #2
0
        public void WaitHandshakeMessage()
        {
            byte[] data = new byte[1024];
            int    bytesCounts;

            try
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    do
                    {
                        bytesCounts = client.Receive(data);
                        memoryStream.Write(data, 0, bytesCounts);
                    } while (client.Available > 0);
                    Message recievedMessage = serializer.MakeDeserialize(memoryStream.ToArray());
                    if (recievedMessage is HandshakeMessage)
                    {
                        HandshakeMessage handshakeMessage = (HandshakeMessage)recievedMessage;
                        clientDataset.clientObject    = this;
                        clientDataset.clientInfo.Name = handshakeMessage.ClientName;
                        clientDataset.clientInfo.Id   = server.id;
                        userName = handshakeMessage.ClientName;
                        Id       = server.id;
                        Console.WriteLine("новый пользователь:" + handshakeMessage.ClientName + "   Id = " + clientDataset.clientInfo.Id.ToString());
                        server.id++;
                        server.AddConnection(clientDataset);
                        ListenTcp();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
        private void CheckSignIn(ref byte[] data, int bytes)
        {
            StringBuilder builder = new StringBuilder();
            string        message = Encoding.UTF8.GetString(data, 1, bytes - 1);

            string[] split = message.Split(":".ToCharArray());
            userName = split[0];
            string userPassword = split[1];

            Console.WriteLine(" login = "******" password = "******"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=UserDB.mdb;";
            OleDbConnection myConnection  = new OleDbConnection(connectString);

            myConnection.Open();
            string       query   = "SELECT COUNT(*) FROM UsersTable WHERE Name=userName AND Password=userPassword";
            OleDbCommand command = new OleDbCommand(query, myConnection);

            command.Parameters.AddWithValue("userName", userName);
            command.Parameters.AddWithValue("userPassword", userPassword);
            if (command.ExecuteScalar() as int? == 1)
            {
                try
                {
                    string outMessage = "21" + userName;
                    byte[] outData    = Encoding.UTF8.GetBytes(outMessage);
                    client.Send(outData);
                    string Mess = "43:" + userName;
                    server.BroadcastMessage(Mess);
                    server.AddConnection(this);
                    client.Send(Encoding.UTF8.GetBytes(CreateMesForUpdateChatOnline()));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                string outMessage = "20" + userName;
                byte[] outData    = Encoding.UTF8.GetBytes(outMessage);
                client.Send(outData);
                Console.WriteLine(" нет такого в бд");
                Close();
            }
            myConnection.Close();
        }