Пример #1
0
        public void Start()
        {
            //start the server
            IPAddress iPAddress = Dns.GetHostEntry(hOST).AddressList[0];

            TcpListener = new TcpListener(iPAddress, pORT);


            try
            {
                TcpListener.Start();
                Console.WriteLine("Server started...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            while (true)
            {
                TcpClient tcpClient = TcpListener.AcceptTcpClient();
                //hand shake
                //Server send public key to client
                SendPublicKey(tcpClient);
                //Server read public key from client
                string publicKeyFromClient = ReadPublicKey(tcpClient);
                //handle login
                //Read username and password from user
                Message request = new Message();
                var     rsa     = new EncryptionRSA();
                rsa.SetPrivateKey(PrivateKey);
                request.ReadFrom(tcpClient.GetStream(), rsa);

                Message response = handlers[request.Header].Invoke(request);

                if (response.Header.Equals("OK"))
                {
                    Console.WriteLine("Client authenticated is connected...");
                    Console.WriteLine("server sends head: " + response.Header + " body " + response.Body);
                    var rsa2 = new EncryptionRSA();
                    rsa2.SetPublicKey(publicKeyFromClient);
                    response.WriteTo(tcpClient.GetStream(), rsa2);
                    RegisterClient(response.Body, publicKeyFromClient, tcpClient);
                    Thread thread = new Thread(() => HandleRequest(tcpClient));
                    thread.Start();
                }
                else
                {
                    var rsa2 = new EncryptionRSA();
                    rsa2.SetPublicKey(publicKeyFromClient);
                    response.WriteTo(tcpClient.GetStream(), rsa2);
                    Console.WriteLine("Client failed to connect");
                }
            }
        }
Пример #2
0
        private void SendPublicKey(TcpClient tcpClient)
        {
            Message msgKey = new Message();

            msgKey.Header = "PUBLIC_KEY";
            msgKey.Body   = PublicKeyToSend;
            msgKey.WriteTo(tcpClient.GetStream());
        }
Пример #3
0
        private void SendPublicKey(TcpClient tcpClient)
        {
            Message msgKey = new Message();

            msgKey.Header = "PUBLIC_KEY";
            msgKey.Body   = PublicKeyToSend;
            Console.WriteLine("Server send key: " + msgKey.Body);
            msgKey.WriteTo(tcpClient.GetStream());
        }
Пример #4
0
        private void broadcast(Message request)
        {
            foreach (Tuple <string, string, TcpClient> client in ActiveClients)
            {
                string publicKey = client.Item2;
                var    rsa       = new EncryptionRSA();
                rsa.SetPublicKey(publicKey);

                request.WriteTo(client.Item3.GetStream(), rsa);
            }
        }