コード例 #1
0
ファイル: file_client.cs プロジェクト: BjornNorgaard/I4IKN
        /// <summary>
        /// Initializes a new instance of the <see cref="file_client"/> class.
        /// 
        /// file_client metoden opretter en peer-to-peer forbindelse
        /// Sender en forspørgsel for en bestemt fil om denne findes på serveren
        /// Modtager filen hvis denne findes eller en besked om at den ikke findes (jvf. protokol beskrivelse)
        /// Lukker alle streams og den modtagede fil
        /// Udskriver en fejl-meddelelse hvis ikke antal argumenter er rigtige
        /// </summary>
        /// <param name='args'>
        /// Filnavn med evtuelle sti.
        /// </param>
        private file_client(String[] args, Transport transportStream)
        {
            try
            {
                Console.WriteLine ("Retrieving file");

                string fileToReceive = (args.Length > 0) ? args[0] : "billede.jpg";

                transportStream.sendText(fileToReceive);

                //Read confirmation that file exists

                if (transportStream.readText() == "File found")
                {
                    receiveFile (fileToReceive, transportStream);
                }
                else
                {
                    Console.WriteLine ("404 - File not found");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine ("Generel exception occured");
                Console.WriteLine(ex.Message);
            }
            finally
            {

            }
        }
コード例 #2
0
ファイル: file_server.cs プロジェクト: BjornNorgaard/I4IKN
        /// <summary>
        /// Initializes a new instance of the <see cref="file_server"/> class.
        /// </summary>
        private file_server(Transport transport)
        {
            try
            {
                Console.WriteLine("Server started - Listening for client");

                //Get filename

                string fileToSend = transport.readText();

                Console.WriteLine("Client trying to retrieve file: " + fileToSend);

                long fileSize = LIB.check_File_Exists (fileToSend);

                if (fileSize != 0)
                {
                    transport.sendText("File found");

                    sendFile (fileToSend, fileSize, transport);
                }
                else
                {
                    transport.sendText("File not found");

                    Console.WriteLine ("404 - File not found");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine (ex.Message);
            }
            finally
            {
                Console.WriteLine ("Exit");
            }
        }
コード例 #3
0
ファイル: file_client.cs プロジェクト: BjornNorgaard/I4IKN
        /// <summary>
        /// Receives the file.
        /// </summary>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='transport'>
        /// Transportlaget
        /// </param>
        private void receiveFile(String fileName, Transport transportStream)
        {
            long fileSize = long.Parse(transportStream.readText());

            Console.WriteLine ("Size of file: " + fileSize);

            byte[] RecData = new byte[BUFSIZE];
            int RecBytes;

            int totalrecbytes = 0;
            FileStream Fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
            while (fileSize > totalrecbytes)
            {
                RecBytes = transportStream.receive (ref RecData);
                Fs.Write (RecData, 0, RecBytes);
                totalrecbytes += RecBytes;
                Console.Write ("\rReceived " + totalrecbytes + " bytes from server");
            }

            Console.WriteLine ("\nFile transfer complete - Closing connection");

            Fs.Close();
        }