private void sendFile(String fileName, long fileSize, Transport transport) { if (fileSize > 0) { int bytesSent = 0; int bytesRead = 0; int bytesLeft = Convert.ToInt32 (fileSize); byte[] dataArray = new byte[BUFSIZE]; byte[] fileSizeByte = new byte[10]; FileStream file = File.OpenRead (fileName); Console.WriteLine ("[FileServer] Opened file: " + fileName); fileSizeByte = BitConverter.GetBytes(fileSize); myTrans.send (fileSizeByte, fileSizeByte.Length); while (bytesLeft > 0) { if (bytesLeft > BUFSIZE) { bytesRead += file.Read (dataArray, bytesSent, BUFSIZE); myTrans.send (dataArray, bytesRead); bytesLeft -= bytesRead; bytesSent += bytesRead; } else { bytesRead += file.Read (dataArray, bytesRead, bytesLeft); // EXEPTION POPS HERE??? myTrans.send (dataArray, bytesRead); bytesLeft -= bytesLeft; } } Console.WriteLine ("[FileServer] File sent."); } }
/// <summary> /// The entry point of the program, where the program control starts and ends. /// </summary> /// <param name='args'> /// First argument: Filname /// </param> public static void Main(string[] args) { Transport trans = new Transport (BUFSIZE); /* byte[] dataToReceive = new byte[BUFSIZE]; Console.WriteLine( "Received number of bytes: " + trans.receive (ref dataToReceive)); for (int i = 0; i < 5; i++) { Console.WriteLine (dataToReceive[i]); } dataToReceive = new byte[BUFSIZE]; Console.WriteLine( "Received number of bytes: " + trans.receive (ref dataToReceive)); for (int i = 0; i < 5; i++) { Console.WriteLine (dataToReceive[i]); } */ new file_client(args, trans); //Console.WriteLine(trans.readText()); //Console.WriteLine(trans.readText()); //trans.sendText ("billede.jpgHELLO THIS IS A TEST MESSAGE"); //trans.sendText ("Test"); }
/// Initializes a new instance of the <see cref="file_server"/> class. /// </summary> private file_client(string[] args) { Buffer = new byte[BUFSIZE]; myTrans = new Transport (BUFSIZE); Console.WriteLine ("[Client] Client_server started."); Console.WriteLine ("[Client] Transport object created."); string filePath = "/root/Documents/I4IKN---gruppe-6/ovelse13/file_server/bin/Debug/hey.txt"; while (true) { nextTry: //Console.WriteLine ("[Client] Input filepath on serverside..."); //string filePath = Console.ReadLine (); int length = filePath.Length; fail: Console.WriteLine ("[Client] Try again? Y/N"); string ch = Console.ReadLine (); if (ch == "y" || ch == "Y") { goto nextTry; } else if (ch == "n" || ch == "N") { receiveFile (filePath, myTrans); } else { goto fail; } Console.WriteLine ("[Client] File Received! \n[Client] Starting over..."); } }
/// <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(); }
/// <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");*/ } }
/// <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 { } }
/// <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 (); }
/// <summary> /// The entry point of the program, where the program control starts and ends. /// </summary> /// <param name='args'> /// The command-line arguments. /// </param> public static void Main(string[] args) { while(true) { Transport trans = new Transport (BUFSIZE); new file_server (trans); } }
/// <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); }
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; }
private file_server () { transport = new Transport(BUFSIZE); encoder = new UTF8Encoding(); try { var fileRequest = ReadString(transport,BUFSIZE); sendFile(fileRequest,fileRequest.Length,transport); } catch (Exception e) { } }
/// <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 (); }
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> /// 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(); }
/// 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."); } }
/// <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(); }
/// <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); } }
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()); }
/// <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> public file_client(String[] args) { var filename = args[0]; var transportLayer = new Transport(BUFSIZE); try { if (args.Length < 1) { Console.WriteLine("You should open the program like ./bip.exe <filename>"); return; } receiveFile(filename, transportLayer); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } }
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); }
/// <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 (); }
private void receiveFile(String filePath, Transport transport) { // Getting filename for placement in client. string fileName = LIB.extractFileName(filePath); string altPath = clientPath + fileName; // Sending to server int length = filePath.Length; byte[] fileMan = new byte[length]; fileMan = Encoding.ASCII.GetBytes (filePath); myTrans.send (fileMan, fileMan.Length); Array.Clear (fileMan, 0, fileMan.Length); myTrans.receive (ref Buffer); int fileSize = BitConverter.ToInt32 (Buffer,0); int bytesReceived = 0; int bytesLeft = fileSize; int bytesWriten = 0; FileStream file = new FileStream(altPath, FileMode.Create, FileAccess.ReadWrite); while (bytesLeft > 0) { Array.Clear (Buffer, 0, Buffer.Length); myTrans.receive (ref Buffer); string test = Encoding.ASCII.GetString (Buffer); Console.WriteLine ("[Client] Writing:" + test); bytesReceived += Buffer.Length; file.Write (Buffer, bytesWriten, bytesReceived); bytesWriten += bytesReceived; bytesLeft -= bytesReceived; } file.Close (); Console.WriteLine ("File is received!"); }
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 (); }
/// <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"); } }
/// <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(); }
/// <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 }
/// <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 }
/// <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> /// 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(); }