Ejemplo n.º 1
0
        bool connectServer()
        {
            System.Console.WriteLine("Please input server's address [and port].");
            string address = System.Console.ReadLine();
            int    port    = 4321;

            if (address.Contains(":"))
            {
                port    = int.Parse(address.Substring(address.IndexOf(':') + 1));
                address = address.Substring(0, address.IndexOf(':') - 1);
            }
            client = new TcpClient(address, port);
            // client.Connect(address,port);
            if (client.Connected)
            {
                stream = client.GetStream();
                status = clientStatus.CONNECTED;
                // Thread t=new Thread(new ThreadStart(this.receiveEvent));
                // t.Join();
                receiveEvent();
                return(true);
            }
            status = clientStatus.UN_CONNECTED;
            return(false);
        }
Ejemplo n.º 2
0
        async void receiveEvent()
        {
            byte[] buf = new byte[MAX_BUF_SIZE];
            //接收密钥
            while (true)
            {
                if (stream.CanRead)
                {
                    int    count = stream.Read(buf, 0, MAX_BUF_SIZE);
                    string recv  = System.Text.Encoding.ASCII.GetString(buf);
                    //密钥以明文形式发送
                    if (recv.Contains("KEY:"))
                    {
                        System.Console.WriteLine(recv);
                        key = Convert.FromBase64String(recv.Substring(4, 12));
                        // System.Console.WriteLine("Key: {0}",Convert.ToBase64String(key));
                        status = clientStatus.ENCRYPTED;
                        break;
                    }
                    else
                    {
                        stream.Write(System.Text.Encoding.ASCII.GetBytes("SEND KEY"), 0, System.Text.Encoding.ASCII.GetByteCount("SEND KEY"));
                    }
                }
            }
            //接收消息循环
            //在服务器端关闭时会报错退出
            try
            {
                while (true)
                {
                    string message = string.Empty;
                    // byte[] buf=new byte[MAX_BUF_SIZE];
                    if (status != clientStatus.ENCRYPTED)
                    {
                        return;
                    }
                    while (true)
                    {
                        int count = await stream.ReadAsync(buf, 0, MAX_BUF_SIZE);

                        message += System.Text.Encoding.ASCII.GetString(buf, 0, count);
                        if (count < MAX_BUF_SIZE)
                        {
                            break;
                        }
                    }
                    // System.Console.WriteLine("Phys Recv: {0}",message);
                    for (int i = 0; i < message.Length; i += 24)
                    {
                        string   part  = message.Substring(i, message.Length - i >= 24 ? 24 : message.Length - i);
                        DESCrypt crypt = new DESCrypt();
                        part = crypt.Decrypt(part, key);
                        System.Console.Write(part);
                    }
                    System.Console.WriteLine();
                    message.Remove(0);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
                stream.Close();
                client.Close();
                Environment.Exit(-1);
            }
        }
Ejemplo n.º 3
0
 //方法
 chatClient()
 {
     status = clientStatus.UN_INITIALIZED;
 }