我想做成像QQ 聊天那样的程序,但对C#网络编程还不太懂,哪位朋友能提一些建议或提供一些学习资料?谢谢了!

解决方案 »

  1.   

    带加密的Chat
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography;namespace Chat
    {
        class ChatApp
        {
            private static IPAddress rempteIPAddress;
            private static int remotePort;
            private static int localPort;
            private static UTF8Encoding Utf8Encod = new UTF8Encoding();
            private static string CryptoKey = "!~6oxli@]t2K'yd$";
            private static string CryptoIV = "!~6oxli@]t2K'yd$";        static void Main(string[] args)
            {
                try
                {
                    //Get necessary data for connection
                    //Console.WriteLine("Enter Local Port");
                    //localPort = Convert.ToInt16(Console.ReadLine());
                    localPort = 10001;                //Console.WriteLine("Enter Remote Port");
                    //remotePort = Convert.ToInt16(Console.ReadLine());
                    remotePort = 10005;
                    
                    //Console.WriteLine("Enter Remote IP Address");
                    //rempteIPAddress = IPAddress.Parse(Console.ReadLine());                rempteIPAddress = IPAddress.Parse("127.0.0.1");
                    //Create thread for listening
                    Thread tRec = new Thread(new ThreadStart(Receive));
                    tRec.Start();                //byte[] bytes = encryptData("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
                    //Console.WriteLine("-----"+bytes.Length.ToString());
                    //bytes = null;
                    //bytes = UTF8Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
                    //Console.WriteLine("======" + bytes.Length.ToString());                //Console.ReadLine();
                    while (true)
                    {
                        Send(Console.ReadLine());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }        private static void Send(string datagram)
            { 
                //Create UdpClient
                UdpClient sender = new UdpClient();
                
                //Create IPEndPoint with details of remote host
                IPEndPoint endPoint = new IPEndPoint(rempteIPAddress, remotePort);            try
                {
                    //byte[] bytes = encryptData(datagram);                byte[] bytes = Encoding.ASCII.GetBytes(datagram);
                    sender.Send(bytes, bytes.Length, endPoint);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally 
                {
                    sender.Close();
                }
            }        private static void Receive()
            {
                //Create a UdpClient for reading incoming data.
                UdpClient receivingUdpClient = new UdpClient(localPort);
                IPEndPoint RemoteIpEndPoint = null;
                try
                {
                    Console.WriteLine("------------------Ready for chat!!!------------------");
                    while (true)
                    { 
                        //wait for datagram
                        byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                        //Convert and display data
                        string returnData = Encoding.ASCII.GetString(receiveBytes);
                        Console.WriteLine("-"+returnData.ToString());
                        //returnData = decryptData(receiveBytes);
                        Console.WriteLine("-"+returnData.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }        static byte[] encryptData(string theDataGram)
            {
                byte[] bCipherText = null;
                try
                {
                    //Create the Rijndael Service Provider object and assign the key and vector to it
                    RijndaelManaged RijndaelProvider = new RijndaelManaged();
                    //Console.WriteLine(RijndaelProvider.BlockSize.ToString() +"/"+ RijndaelProvider.Key.Length + "/" + RijndaelProvider.IV.Length);                //Console.WriteLine(UTF8Encoding.ASCII.GetBytes(CryptoIV).Length.ToString());
                    RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey);
                    //Utf8Encod = new UTF8Encoding();
                    RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV);                ICryptoTransform RijndaelEncrypt = RijndaelProvider.CreateEncryptor();
                    byte[] bClearText = Utf8Encod.GetBytes(theDataGram);
                    MemoryStream Mstm = new MemoryStream();
                    //Create Crypto Stram that transforms a stream using the encryption
                    CryptoStream Cstm = new CryptoStream(Mstm, RijndaelEncrypt, CryptoStreamMode.Write);                //Write out encrypted content into MemoryStream
                    Cstm.Write(bClearText, 0, bClearText.Length);
                    Cstm.FlushFinalBlock();                //Get the output
                    bCipherText = Mstm.ToArray();
                    Cstm.Close();
                    Mstm.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                return bCipherText;
            }        static string decryptData(byte[] bCipherText)
            {
                string sEncoded = "";
                try
                {
                    //Create the RijndaelManaged Service Provider object and assign the key and vector to it
                    RijndaelManaged RijndaelProvider = new RijndaelManaged();
                    RijndaelProvider.Key = Utf8Encod.GetBytes(CryptoKey);
                    RijndaelProvider.IV = Utf8Encod.GetBytes(CryptoIV);                ICryptoTransform RijndaelDecrypt = RijndaelProvider.CreateDecryptor();
                    MemoryStream Mstm = new MemoryStream(bCipherText, 0, bCipherText.Length);
                    //Create Crypto Stram that transforms a stream using the encryption
                    CryptoStream Cstm = new CryptoStream(Mstm, RijndaelDecrypt, CryptoStreamMode.Read);                //Read out the result from the Crypto Stream
                    StreamReader Sr = new StreamReader(Cstm);
                    sEncoded = Sr.ReadToEnd();                Sr.Close();
                    Cstm.Close();
                    Mstm.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                return sEncoded;
            }
        }
    }
      

  2.   

    三种方案
    1.dot net remoting;
    2.web severce
    3.socket