Exemple #1
0
        /// <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
            {

            }
        }
Exemple #2
0
        /// <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");
            }
        }
Exemple #3
0
        /// <summary>
        /// Sends the file.
        /// </summary>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='fileSize'>
        /// File size.
        /// </param>
        /// <param name='tl'>
        /// Tl.
        /// </param>
        private void sendFile(String fileName, long fileSize, Transport transport)
        {
            FileStream fileStream = null;

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

                transport.sendText(fileSize.ToString());

                byte[] SendingBuffer = null;

                fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(fileStream.Length) / Convert.ToDouble(BUFSIZE)));

                int TotalLength = (int)fileStream.Length;
                int CurrentPacketLength, bytesSent = 0;

                for (int i = 1; i < NoOfPackets+1; i++)
                {
                    if (TotalLength > BUFSIZE)
                    {
                        CurrentPacketLength = BUFSIZE;
                        TotalLength = TotalLength - CurrentPacketLength;
                        bytesSent += BUFSIZE;
                    }
                    else
                    {
                        CurrentPacketLength = TotalLength;
                        bytesSent += CurrentPacketLength;
                    }

                    SendingBuffer = new byte[CurrentPacketLength];
                    fileStream.Read(SendingBuffer, 0, CurrentPacketLength);
                    transport.send(SendingBuffer, (int)SendingBuffer.Length);

                    Console.Write("\rSent " + i + " of " + NoOfPackets + " packets to client. Total of " + bytesSent + " bytes sent");
                }
                Console.WriteLine ("\nFile sent - closing connection");
            }
            catch (Exception ex)
            {
                Console.WriteLine (ex.Message);
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close ();
            }
        }