Beispiel #1
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 = m_bufferSize;
                _clientSocket.SendBufferSize    = m_bufferSize;
                // start receiving
                _clientSocket.ReceiveAsync();
                _clientSocket.ReceiveTimeout = -1;
                // connection established
                _isConnected = true;
            }
        }
        private async Task ReceiveData(byte[] buffer, List <byte> backlog, CancellationToken token)
        {
            var byteCount = await tcpSocket.ReceiveAsync(buffer, 0, MAXIMUM_BUFFER_SIZE);

            if (byteCount == 0) // The remote has shutdown the socket.
            {
                throw new TcpSocketIsClosedException();
            }
            byte[] data = new byte[byteCount];
            Array.Copy(buffer, 0, data, 0, byteCount);
            backlog.AddRange(data);
        }