コード例 #1
0
        public void UDP_Client(UdpClient _udpClient, ClientServer clientServer, RSAParameters rsaPrivateParams)
        {
            udpClient      = _udpClient;
            rsaPrivatePara = rsaPrivateParams;
            csData         = clientServer;
            //获取目标UDP IP&Port
            clientEndPoint = clientServer.udpIPPort;
            //获取客户端IP和Port
            while (true)
            {
                Console.WriteLine(" 1 Send Message To UDP");
                Console.WriteLine(" 2 Close Connect");
                int Num = Convert.ToInt32(Console.ReadLine());
                switch (Num)
                {
                case 1:
                    ClientSendClient();
                    break;

                case 2:
                    ClientClose();
                    break;
                }
            }
        }
コード例 #2
0
        public void DataComm(object obj_clientSocket)
        {
            Socket   clientSocket   = obj_clientSocket as Socket;
            EndPoint remoteEndPoint = clientSocket.RemoteEndPoint;
            //接收客户端信息
            int bufferLength = 0;//= clientSocket.Receive(buffer);

            while (true)
            {
                try
                {
                    bufferLength = clientSocket.Receive(buffer);
                }
                catch (Exception ex)
                {
                    //关闭连接时出错,不做任何反应
                    Console.WriteLine("Client Closed...");
                }
                //创建内存流
                MemoryStream    mStream    = new MemoryStream();
                BinaryFormatter bFormatter = new BinaryFormatter();
                mStream.Write(buffer, 0, bufferLength);
                mStream.Flush();
                mStream.Position = 0;
                //数据反序列化
                ClientServer cs = bFormatter.Deserialize(mStream) as ClientServer;

                /*bool isClient = false;
                 * foreach (ClientServer c in csList)
                 * {
                 *  if (c.tcpIPPort == cs.tcpIPPort)
                 *  {
                 *      isClient = true;
                 *  }
                 * }*/
                if (cs.commands == "SendCleint")
                {
                    csList.Add(cs);
                    Console.WriteLine("Server receive {0} Sucess", csList.Count);
                }
                if (cs.commands == "GetList")
                {
                    //发送客户端列表
                    MemoryStream    mStream_    = new MemoryStream();
                    BinaryFormatter bFormatter_ = new BinaryFormatter();
                    //对象序列化
                    bFormatter_.Serialize(mStream_, csList);
                    mStream_.Flush();
                    mStream_.Position = 0;
                    //Send
                    clientSocket.Send(mStream_.GetBuffer(), (int)mStream_.Length, SocketFlags.None);
                }
                Console.WriteLine("List {0}", csList.Count);
            }
        }
コード例 #3
0
        private static void UDPReceiveServer(object client)
        {
            UdpClient udpServer = client as UdpClient;

            Console.WriteLine("UDP Server Enable........");
            while (true)
            {
                //Console.WriteLine("等待接收数据");
                IPEndPoint clientEndPoint = null;
                try
                {
                    buffer = udpServer.Receive(ref clientEndPoint);
                }catch (Exception ex)
                {
                    Console.WriteLine("Client Closeing");
                }
                //对buffer数据进行认证和
                MemoryStream    mStream    = new MemoryStream();
                BinaryFormatter bFormatter = new BinaryFormatter();
                mStream.Write(buffer, 0, buffer.Length);
                mStream.Flush();
                mStream.Position = 0;
                ClientServer cs = bFormatter.Deserialize(mStream) as ClientServer;
                //解密
                //设置DES脱密密钥
                Console.WriteLine("Please Input Key");
                //string KEY_64 = Console.ReadLine();
                string KEY_64 = "A4G-8=Jk";
                Console.WriteLine(KEY_64);
                //设置初始化向量
                Console.WriteLine("PLease INput IV");
                //string IV_64 = Console.ReadLine();
                string IV_64 = "JKbN=5[?";
                Console.WriteLine(IV_64);
                byte[] KeyBytes = Encoding.UTF8.GetBytes(KEY_64);
                byte[] IVBytes  = Encoding.UTF8.GetBytes(IV_64);
                //先DES解密
                byte[] plainData  = DES_C_C.TransformBuffer(cs.dasEncryptData, "Decrypt", KeyBytes, IVBytes);
                string receiveMsg = Encoding.UTF8.GetString(plainData);
                //验证签名
                //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(plainData));
                //Console.WriteLine("UDP Server Sign Data*****: {0}", BitConverter.ToString(cs.rsaSignData));
                //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(cs.dasEncryptData));

                //Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(cs.clientRSAPublicPar.Exponent));
                //Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(cs.clientRSAPublicPar.Modulus));
                //对数据进行验证签名
                bool   isRight      = RSA_C_S.RsaVerifyData(plainData, cs.clientRSAPublicPar, cs.rsaSignData);
                string VerifyResult = isRight ? "YES" : "NO";
                Console.WriteLine("Server Message****Verify Result :{0}", VerifyResult);
                Console.WriteLine("Server Message****Receive From {0} Message:{1} ", clientEndPoint.ToString(), receiveMsg);
            }
        }
コード例 #4
0
        private static void ClientToClient()
        {
            foreach (ClientServer _cs in csList)
            {
                if (_cs.tcpIPPort.ToString() == clientSocket.LocalEndPoint.ToString())
                {
                    Console.WriteLine("{0} is Client {1} LocalHost!!", csList.IndexOf(_cs), _cs.tcpIPPort.ToString());
                    continue;
                }
                Console.WriteLine("{0} Client {1}", csList.IndexOf(_cs), _cs.tcpIPPort.ToString());
            }
            Console.WriteLine("Please Select Client Number:");
            int number = Convert.ToInt32(Console.ReadLine());

            //set number check
            c_s = csList[number];
            Console.WriteLine(c_s.tcpIPPort.ToString());
            //创建UDP Client
            new UDPClient().UDP_Client(udpClient, c_s, rsaPrivateParameter);
        }