C# 底层协议开发(1)

     何为协议?先撇开专业术语不说,顾名思义,就是相互合作的暗语(不怕公开的话),

比如,假如我俩商量好了,1+1=22,当我写1+1的时候,你如果写2,那就

违反协议了...

     同理,做底层开发的时候,先必须定义好 Client 与  Server(或者 Client) 的协作符号,

比如,

A : 要計算1+1=22;

B: 要計算2+2=44;

exit :  退出;

 

用C#開發,,代碼如下:

 

    class Program
    {

        static void Main(string[] args)
        {
            AppDomain.CreateDomain("socketServer").DoCallBack(
                delegate
                {
                     byte[] result = new byte[1024];
                     int myport = 8889;

                     IPAddress ip = IPAddress.Parse("192.168.2.136");
                    IPEndPoint ipe = new IPEndPoint(ip,myport);
                     Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                         ProtocolType.Tcp);
                     serverSocket.Bind(ipe);
                     serverSocket.Listen(1);
                     Console.WriteLine("已經啟動監聽!");
                     Thread myThread = new Thread(new ThreadStart(delegate {
                         while (true)
                         {
                             Socket clientsocket = serverSocket.Accept();
                             clientsocket.Send(Encoding.UTF8.GetBytes("您好!,您已經與我取得聯繫了!"));

                             Thread receiveThread = new Thread(delegate(object clientSocket) {
                                 Socket myClientSocket = (Socket)clientSocket;
                                 string casestr ="";
                                 while (true)
                                 {
                                     int receiveNumber = myClientSocket.Receive(result);
                                     //Console.WriteLine("接受客戶端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(result, 0, receiveNumber));
                                     casestr = Encoding.UTF8.GetString(result, 0, receiveNumber);
                                     switch ( casestr)
                                     {
                                         case "A":
                                             Console.WriteLine("1+1=22");
                                             break;
                                         case "B":
                                             Console.WriteLine("2+2=44");
                                             break;
                                         default:
                                             Console.WriteLine("無此協議");
                                             break;
                                     }
                                        

                                 }
                             });

                             receiveThread.Start(clientsocket);
                         }
                           
                     }));
                     myThread.Start();
                });

            IPAddress IPCL = IPAddress.Parse("192.168.2.136");
            Socket CLSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            CLSocket.Connect(new IPEndPoint(IPCL,8889));
            Console.WriteLine("鏈接服務成功\r\n");
            byte[]rs = new byte[1024];
            int receivecount = CLSocket.Receive(rs);
            Console.WriteLine("接受消息{0}\r\n", Encoding.UTF8.GetString(rs, 0, receivecount));

            //CLSocket.Send(Encoding.UTF8.GetBytes("我是客戶端!\r\n"));
            string str = "A";
            while (true)
            {
                if (str == "exit") return;
                CLSocket.Send(Encoding.UTF8.GetBytes(str ));
                str = Console.ReadLine();
            }

 
        }

 

 

posted on 2012-01-16 10:46  Mayvar  阅读(784)  评论(0编辑  收藏  举报

导航