Esempio n. 1
0
		/// <summary>
		/// Initializes a new instance of the <see cref="file_server"/> class.
		/// </summary>
		private file_server ()
		{
			// TO DO Your own code
			int size=0;

			while (true) 
			{
				try 
				{
					_transport = new Transport (BUFSIZE);
					//byte[] fileToSend = new byte[BUFSIZE];

					byte[] fileToSend = new byte[BUFSIZE];
					while((size = _transport.receive(ref fileToSend)) == 0);
					string stringToSend = "";

					for (int i = 0; i < size; i++)
					{
						stringToSend += (char)fileToSend[i];
					}

					Console.WriteLine("Filename: " + stringToSend);
					sendFile (stringToSend, _transport);
				} 
				catch (TimeoutException) 
				{
					Console.WriteLine ("Timed out...");
				}
			}	

		}
        /// <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");*/
            }
        }
Esempio n. 3
0
		internal string ReadString(Transport t, int buffersize)
		{
			byte[] bytes = new byte[buffersize];
			int size = t.receive(ref bytes);
			string returnstring = "";

			for (int i = 0; i < size; i++)
			{
				returnstring += (char)bytes[i];
			}

			return returnstring;
		}
Esempio n. 4
0
		/// <summary>
		/// Receives the file.
		/// </summary>
		/// <param name='fileName'>
		/// File name.
		/// </param>
		/// <param name='transport'>
		/// Transportlaget
		/// </param>
		private void receiveFile (String fileName, Transport transport)
		{
			// TO DO Your own code
			byte[] bytesForSize = new byte[BUFSIZE];
			int fsize = transport.receive(ref bytesForSize);
			string stringToSend = "";

			for (int i = 0; i < fsize; i++)
			{
				stringToSend += (char)bytesForSize[i];
			}


			long fileSize = long.Parse (stringToSend);
			
			if (fileSize == 0) 
			{
				Console.WriteLine ("Filelength was 0, doesnt exist...");
				return;
			}

			Console.WriteLine ("Creating file...");
			var fileToRecieve = File.Create (fileName);

			var bytesToRecieve = new byte[BUFSIZE];
			int index = 0;

			while (index < fileSize) 
			{
				int size = transport.receive (ref bytesToRecieve);
				fileToRecieve.Write (bytesToRecieve, 0, size);
				Console.WriteLine ("{0} bytes written to file", size);
				index += size;
			}

			fileToRecieve.Close ();
		}
        /// <summary>
        /// Receives the file.
        /// </summary>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='transport'>
        /// Transportlaget
        /// </param>
        private void receiveFile(string fileName, Transport transport)
        {
            // TO DO Your own code
            byte[] recieveSize = new byte[100];
            int recievedSizeLength = transport.receive(ref recieveSize);
            long fileSize = long.Parse(LIB.GetString(recieveSize));

            if (fileSize < 1) {
                Console.WriteLine ("File not found on server");
                return;
            }

            Console.WriteLine(fileSize);
            string filePath = @"/home/ikn/Desktop/" + fileName;

            FileStream fileStream = new FileStream(filePath, FileMode.Append);
            BinaryWriter binWriter = new BinaryWriter(fileStream);

            bool run = true;

            while (run)
            {
                long size;
                if (fileSize < BUFSIZE)
                    size = fileSize;
                else
                    size = BUFSIZE;

                fileSize -= size;

                byte[] byteFile = new byte[size];

                transport.receive(ref byteFile);

                binWriter.Write(byteFile);

                if (fileSize < 1)
                {
                    run = false;
                }
            }
            binWriter.Close();
            fileStream.Close();
        }
Esempio n. 6
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.");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="file_server"/> class.
        /// </summary>
        public file_server()
        {
            Console.WriteLine("Waiting for request");
            var _tran = new Transport(BUFSIZE);

            Byte[] bytes = new Byte[BUFSIZE];
            string data;

            int i = _tran.receive(ref bytes);

            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
            //string filename = ((new UTF8Encoding()).GetString(bytes)).Substring(0, i);

            Console.WriteLine("Received: {0}", data);

            if (File.Exists(data))
            {
                sendFile(data, data.Length, _tran);
            }
        }
Esempio n. 8
0
		private void receiveFile (String fileName, Transport transport)
		{
			long fileLength = long.Parse(ReadString(transport,BUFSIZE));

			if (fileLength == 0) 
			{
				Console.WriteLine ("File does not exist");
				return;
			}

			byte[] b = new byte[BUFSIZE];
			FileStream f = File.Create (fileName);

			int i = 0;
			while (i < fileLength) 
			{
				int size = transport.receive(ref b);
				//Console.WriteLine (size);
				f.Write (b, 0, size);
				i += size;
			}

			f.Close ();
		}
Esempio n. 9
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();
        }
Esempio n. 10
0
        /// <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();
        }