예제 #1
0
파일: DtmKex.cs 프로젝트: modulexcite/CEX
        /// <summary>
        /// Connect to a server and begin the key exchange
        /// </summary>
        /// 
        /// <param name="Address">The servers IP Address</param>
        /// <param name="Port">The servers Port number</param>
        /// <param name="Async">Connect on a non-blocking TCP channel</param>
        /// 
        /// <exception cref="CryptoNetworkingException">Thrown if a socket error is returned</exception>
        public void Connect(IPAddress Address, int Port, bool Async = true)
        {
            // create the connection
            _clientSocket = new TcpSocket();
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnClientConnected);
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);

            try
            {
                if (Async)
                    _clientSocket.ConnectAsync(Address, Port);
                else
                    _clientSocket.Connect(Address, Port);
            }
            catch (Exception ex)
            {
                if (SessionError != null)
                    SessionError(this, new DtmErrorEventArgs(new CryptoSocketException("DtmKex:Connect", "The connection attempt has failed", ex), DtmErrorSeverity.Connection));
            }
        }
예제 #2
0
        public void StartReceive(IPAddress Address, int Port, string FilePath)
        {
            // store destination path
            _filePath = FilePath;
            // initialize the file socket
            _clientSocket = new TcpSocket();
            // use the FileDataReceived callback
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnConnected);
            _clientSocket.DisConnected += new TcpSocket.DisConnectedDelegate(OnDisConnected);
            _clientSocket.Connect(Address, Port);

            if (!_clientSocket.IsConnected)
            {
                // connect attempt failed
                throw new CryptoSocketException("DtmFileTransfer:StartReceive", "Could not connect to the remote host!", new SocketException((int)SocketError.ConnectionAborted));
            }
            else
            {
                // create the temp file  note: is WriteThrough more secure here?
                _tempPath = Path.Combine(Path.GetDirectoryName(_filePath), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".tmp");
                using (new FileStream(_tempPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { }
                // set to hidden to avoid cross process errors
                File.SetAttributes(_tempPath, File.GetAttributes(_tempPath) | FileAttributes.Hidden);
                _clientSocket.ReceiveBufferSize = _bufferSize;
                _clientSocket.SendBufferSize = _bufferSize;
                // start receiving
                _clientSocket.ReceiveAsync();
                _clientSocket.ReceiveTimeout = -1;
                // connection established
                _isConnected = true;
            }
        }