/// <summary>
        /// Initializes a new instance of the <see cref="file_server"/> class.
        /// </summary>
        private file_server()
        {
            // TO DO Your own code
            byte[] fileName = new byte[BUFSIZE];
            Transport transport = new Transport(BUFSIZE);

            Console.WriteLine("Waiting for client...");
            var fileLength = transport.receive(ref fileName);

            Console.WriteLine("Server recieved " + LIB.GetString(fileName) + " from client.");

            var actualFileName = new byte[fileLength];
            Array.Copy(fileName, actualFileName, fileLength);
            long fileSize = LIB.check_File_Exists(LIB.GetString(actualFileName));

            if (fileSize > 0)
            {
                transport.send(LIB.GetByteArray(fileSize.ToString()), LIB.GetByteArray(fileSize.ToString()).Length);
                SendFile(LIB.GetString(actualFileName), fileSize, transport);
            }
            else
            {
                transport.send(LIB.GetByteArray("0"), LIB.GetByteArray("0").Length);
                /*var send = LIB.GetByteArray("File not exist");*/
            }
        }
		/// <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, Transport transport)
		{
			long fileLength = LIB.check_File_Exists(fileName);
			
			System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
			var stringToSend = fileLength.ToString ();
			_transport.send(encoding.GetBytes(stringToSend), stringToSend.Length);	

			if (fileLength == 0) 
			{
				Console.WriteLine ("Filelength was 0, does not excist");
				return;
			}

			FileStream file = File.Open (fileName, FileMode.Open);

			byte[] data = new byte[BUFSIZE];
			for (int i = 0; i < fileLength; i += BUFSIZE) 
			{
				int size = file.Read (data, 0, BUFSIZE);
				Console.WriteLine (size);
				transport.send (data, size);
			}
			file.Close ();
		}
		private void sendFile(String fileName, long fileSize, Transport transport)
		{
			fileSize = LIB.check_File_Exists (fileName);
			transport.send(encoder.GetBytes (fileSize.ToString()), fileSize.ToString().Length);

			if (fileSize == 0)
				return;

			FileStream f = File.Open (fileName, FileMode.Open);

			byte[] b = new byte[BUFSIZE];

			for (int i = 0; i < fileSize; i += BUFSIZE) 
			{
				int size = f.Read (b, 0, BUFSIZE);
					transport.send (b, size);
			}
		}
        /// <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)
        {
            // TO DO Your own code
            Transport transport = new Transport(BUFSIZE);
            string file = @"/home/ikn/Desktop/test.png";
            byte[] fileName = LIB.GetByteArray(file);
            transport.send(fileName, fileName.Length);

            receiveFile(LIB.extractFileName(file), transport);
        }
		/// <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 _transport = new Transport (BUFSIZE);
			string filename = args[0];
			
			try{						
				System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
				_transport.send(encoding.GetBytes(filename), filename.Length);							
			}
			catch(TimeoutException){
				Console.WriteLine("Receive timed out");
			}

			receiveFile (LIB.extractFileName(filename), _transport);
			Console.WriteLine ("Done!");
			Console.ReadKey ();
	    }
Example #6
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)
        {
            var reader = new BinaryReader(File.Open(fileName, FileMode.Open));

            int counter = 1;
            byte[] array;

            do
            {
                array = reader.ReadBytes(1000);
                transport.send(array, array.Length);

                //Console.WriteLine("#{0} - {1} bytes", counter, array.Length);

                counter++;
            } while (array.Length > 0);

            reader.Close();
        }
Example #7
0
        /// Initializes a new instance of the <see cref="file_server"/> class.
        /// </summary>
        private file_server()
        {
            receiveBuffer = new byte[BUFSIZE];
            myTrans = new Transport (BUFSIZE);
            Console.WriteLine ("[FileServer] Server started.");
            Console.WriteLine ("[FileServer] Transport object created.");

            while (true) {
                bool failFlag = false;
                Console.WriteLine ("[FileServer] Waiting to receive filename.");

                myTrans.receive (ref receiveBuffer);

                if (failFlag == false) {
                    string temp = Encoding.ASCII.GetString(receiveBuffer);
                    string filepath = "";

                    for (int i = 0; i < temp.Length; i++) {
                        char ch = temp[i];
                        if (ch != '\0') {
                            filepath += ch;
                        }
                    }

                    Console.WriteLine ("[FileServer] Filepath received: ");
                    Console.WriteLine (filepath);

                    long fileSize;

                    fileSize = LIB.check_File_Exists (filepath);

                    if (fileSize > 0) {
                        Console.WriteLine ("[FileServer] Sending file!");
                        sendFile (filepath, fileSize, myTrans);
                    } else {
                        receiveBuffer = BitConverter.GetBytes(fileSize);
                        myTrans.send (receiveBuffer, receiveBuffer.Length);
                        Console.WriteLine ("[FileServer] File doesn't exist.");
                    }
                }
                Console.WriteLine ("[FileServer] Starting over.");
            }
        }
Example #8
0
        public test_main()
        {
            int BUFSIZE = 1000;
            Transport myTrans = new Transport (BUFSIZE);

            Console.WriteLine ("Write some letters. ");
            string test = Console.ReadLine ();
            int length = test.Length;
            Console.Write (test, test.Length);
            byte[] A = new byte[3];

            for (int i = 0; i < 3; i++) {
                A [i] = (byte)'A';
            }

            myTrans.send (A,A.Length);

            Console.WriteLine ("Output: ");
            Console.WriteLine (A.ToString());
        }
	    private file_client(String[] args)
	    {
			transport = new Transport (BUFSIZE);
			encoder = new UTF8Encoding();
			var filePath = encoder.GetBytes(args [0]);

			try
			{
				transport.send(filePath,filePath.Length);
			}
			catch(Exception e) 
			{
				Console.WriteLine ("Server not responding...");
			}
//			string fP = "";
//			for (int i = 0; i < filePath.Length; i++) 
//			{
//				fP += filePath [i];
//			}
			receiveFile (args[0], transport);
	    }
Example #10
0
        /// <summary>
        /// Receives the file.
        /// </summary>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='transport'>
        /// Transportlaget
        /// </param>
        private void receiveFile(String fileName, Transport transport)
        {
            // Translate the passed message into ASCII and store it as a Byte array.
            byte[] data = System.Text.Encoding.ASCII.GetBytes(fileName);

            transport.send(data, data.Length);

            // Remove path from filename.
            string file = Path.GetFileName(fileName);
            // Create BinaryWriter to write the read data to a file.
            var writer = new BinaryWriter(File.Open(file, FileMode.Create));
            int i, counter = 1;
            data = new byte[1000];
            do
            {
                i = transport.receive(ref data);
                writer.Write(data, 0, i);

                Console.WriteLine("#{0} - {1} bytes", counter, i);

                counter++;
            } while (i > 0);

            writer.Close();
        }
Example #11
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 ();
            }
        }
        /// <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)
        {
            // TO DO Your own code
            byte[] file = new byte[BUFSIZE];
            FileStream fileStream = new FileStream(fileName, FileMode.Open);
            BinaryReader binReader = new BinaryReader(fileStream);

            bool run = true;
            while (run)
            {
                file = binReader.ReadBytes(BUFSIZE);
                transport.send(file, file.Length);
                fileSize -= BUFSIZE;
                if (fileSize < BUFSIZE)
                {
                    file = binReader.ReadBytes((int)fileSize);
                    transport.send(file, file.Length);
                    run = false;
                }

            }
            binReader.Close();
            fileStream.Close();
        }