Ejemplo n.º 1
0
        /// <summary>
        /// 连接函数
        /// </summary>
        /// <param name="host">服务器地址</param>
        /// <param name="port">服务器端口</param>
        public void Connect(string host, int port)
        {
            this.host = host;
            this.port = port;
            if (isOpen)
            {
                throw new TransmissionException("已经连接了服务器!");
            }
            try
            {
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(host, port);
                channelHandler = new PacketHandler(client);
                key            = channelHandler.Key;
                iv             = channelHandler.IV;

                byte[] handshakeMsg = HandshakeHandler.BuildHandshake(key, iv);
                channelHandler.SendNormalPacket(handshakeMsg);
                byte[] ackBytes = channelHandler.ReceiveSealedPacket();
                if (HandshakeHandler.VerifyHandshake(ackBytes, key, iv))
                {
                    isOpen = true;
                }
                else
                {
                    channelHandler.Close();
                }
            }
            catch (SocketException se)
            {
                throw new TransmissionException(se.Message, se);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送一般传输包
 /// </summary>
 /// <param name="packet">发送的一般传输包</param>
 public void SendNormalPacket(byte[] packet)
 {
     handler.SendNormalPacket(packet);
     foreach (PacketLogger logger in loggers)
     {
         logger.Write(packet, EndPoint);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 记录数据包
        /// </summary>
        /// <param name="buffer">待记录的数据包</param>
        /// <param name="endPoint">IP端点</param>
        public override void Write(byte[] buffer, IPEndPoint endPoint)
        {
            DictNode node = new DictNode();

            node.Add("buffer", buffer);
            node.Add("endpoint", endPoint.ToString());
            byte[] sndMsg = BEncode.ByteArrayEncode(node);
            handler.SendNormalPacket(sndMsg);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 打开连接
 /// </summary>
 public void Open()
 {
     if (isOpen)
     {
         throw new DbProxyException("连接已经打开!");
     }
     socket.Connect(url, port);
     socket.ReceiveTimeout = timeout;
     handler = new PacketHandler(socket);
     byte[] sndMsg = BuildOpenPacket();
     handler.SendNormalPacket(sndMsg);
     byte[] rcvMsg = handler.ReceiveNormalPacket();
     isOpen = CheckOpenPacket(rcvMsg);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 连接函数
        /// </summary>
        /// <param name="host">服务器地址</param>
        /// <param name="port">服务器端口</param>
        public void Connect(string host, int port)
        {
            if (isOpen)
            {
                throw new TransmissionException("已经连接了服务器!");
            }
            try
            {
                Socket     server         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, clientPort);
                server.Bind(serverEndPoint);
                server.Listen(1);

                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(host, port);
                channelHandler1 = new PacketHandler(client);
                key             = channelHandler1.Key;
                iv = channelHandler1.IV;

                byte[] openBytes = BuildHandshakeMessage();
                channelHandler1.SendNormalPacket(openBytes);

                Socket socket = server.Accept();
                channelHandler2 = new PacketHandler(socket);
                channelHandler2.SetKeyIV(key, iv);
                byte[] ackMsg = channelHandler2.ReceiveSealedPacket();
                if (HandshakeHandler.VerifyHandshake(ackMsg, key, iv))
                {
                    isOpen = true;
                    channelHandler1.SendSealedPacket(ackMsg);
                }
                else
                {
                    channelHandler1.Close();
                    channelHandler2.Close();
                }
            }
            catch (SocketException se)
            {
                throw new TransmissionException(se.Message, se);
            }
        }