예제 #1
0
        // 客户端 client 的接受信息的方法
        private void ReceiveData(object client)
        {
            Socket nowClient = (Socket)client;
            Cer dataC = new Cer();
            while (true)
            {
                int res = 0;
                byte[] bytes = new byte[1024];
                // 不段的接受客户端发来的信息, 当客户端离线后,退出。
                try
                {
                    res = nowClient.Receive(bytes);
                }
                catch
                {
                    // client 离线,更新userNum 值
                    userNum--;
                    Console.WriteLine("现在有:{0} 个客户在连接中。", userNum);
                    string str1 = "现在有:" + userNum + "个客户在连接中";
                    text1(str1);
                    return;
                }

                string str = Encoding.UTF8.GetString(bytes, 0, res);
                Console.WriteLine("{0}", str);//接收信息
                //信息接收处理函数(所有信息类型,均在此处理)
               dataC= inf.getClient(str,dataC);
               if (dataC.check == "RequestOK")
                {
                    string str2=dataC.other;
                    Byte[] data = Encoding.UTF8.GetBytes(str2);
                    SendAllUser(data, nowClient);
                    dataC.other = null;
                    dataC.check = null;
                }
               else if (dataC.check == "SendOK")
               {
                   textboxdencode(dataC.ID+"向你发送了:"+dataC.other);
                   dataC.other = null;
                   dataC.check = null;

               }
                else
                {
                    text1(dataC.other);
                    dataC.other = null;
                  //  break;
                }
                //将该信息发送给其他客户端
                textboxrev(str);
            }
            //        nowClient.Close();
        }
예제 #2
0
 //会话请求拆包
 public Cer getClient(string msg,Cer data)
 {
     Cer a = new Cer();
     a = data;
     string m=null;
     string[] array = msg.Split(';');
     int lengthStr = array.Length;
        //接受加密会话信息
        if(array[0]=="s")
        {
        if(lengthStr!=3)
        {
            //请求报文缺失
            a.other = "请求报文缺失";
            return a;
        }
        string desKey = ser.Decrypt(array[1], PrivateKey, nKey);
        Des des = new Des();
        a.other=des.Decrypt(array[2],desKey);
        a.check = "SendOK"; //代表加密拆解正常
        return a;
        }
        //接受会话请求(证书交换)
        else if(array[0]=="r")
        {
        if(lengthStr!=9)
        {
            //请求会话包报文缺失
            a.other = "请求会话包报文缺失";
            return a;
        }
        //判断证书
        int s=ser.check("CA",array[7],array[5],array[6]);
        if(s==1)
        {
            //判断签名
            //可在此添加版本
            if(ser.check(array[2],array[8],array[3],array[4])==1)
            {
                a.ekey = array[3];
                a.nkey = array[4];
                a.ID = array[2];
                a.other = "c;" + cer + ";" + sign;
                a.check = "RequestOK"; //请求回话成功
                return a;
            }
            else
            {
                //假签名
                a.other = "签名错误";
                return a;
            }
        }
        else
        {
            //假证书
            a.other = "证书错误";
            return a;
        }
        }
        else
        {
        a.other = "报文格式错误";
        return a;
        }
 }