/// <summary> /// send the content of the filepath trough the socket per chunk /// </summary> /// <param name="s">socket that's open where to send the file</param> /// <param name="filePath">path of the file</param> private void sendThroughSocket(Socket s, string filePath) { ProgressDialogue progressDialogue = new ProgressDialogue("Uploading...", "transfering...", 0); Thread threadProgress; threadProgress = new Thread(() => UploadProgressBarThread(progressDialogue)); threadProgress.IsBackground = true; threadProgress.Start(); Thread.Sleep(500); using (var file = File.OpenRead(filePath)) { byte[] sendBuffer = new byte[BUFFER_SIZE]; long bytesLeftToTransmit = file.Length; double fileLengthMo = (double)file.Length / 1000000; while (bytesLeftToTransmit > 0) { int dataToSend = file.Read(sendBuffer, 0, sendBuffer.Length); bytesLeftToTransmit -= dataToSend; s.Send(sendBuffer); int percentage = Convert.ToInt32((((double)file.Length - (double)bytesLeftToTransmit) / (double)file.Length) * (double)100); progressDialogue.SetProgress(percentage); progressDialogue.ChangeText("Uploading...\n" + (((double)file.Length - (double)bytesLeftToTransmit) / 1000000).ToString("f2") + " / " + fileLengthMo.ToString("f2") + "Mo"); } sendBuffer = null; progressDialogue.CloseForm(); } }
//write the received file public void WriteFile(string path) { this.clientSocket.Send(Encoding.UTF8.GetBytes("accepted<EOF>")); bool isConvertible = false; double length = 0; int operation = 1; ProgressDialogue progressDialogue = new ProgressDialogue("Downloading...", "transfering...", 0); Thread threadProgress; threadProgress = new Thread(() => DownloadProgressBarThread(progressDialogue)); threadProgress.IsBackground = true; threadProgress.Start(); while (!isConvertible) { try { length = Convert.ToDouble((double)nbo / operation); isConvertible = true; } catch (OverflowException e) { isConvertible = false; if (operation == 1) { operation = 1024; } else { operation = Convert.ToInt32(Math.Pow(operation, 2)); } } } FileInfo info = new FileInfo(path); FileStream Stream = new FileStream(path, FileMode.Create); double bytesLeftToReceive = length; byte[] receiveBuffer = new byte[5000000]; int offset = 0; //receive the file content while (bytesLeftToReceive > 0) { int bytesRead = clientSocket.Receive(receiveBuffer); //if we don't do that the file will have a lot of zeroes at the end because it will read all the receiveBuffer if (bytesRead / operation > bytesLeftToReceive) { Stream.Write(receiveBuffer, 0, Convert.ToInt32(bytesLeftToReceive * operation)); } else { Stream.Write(receiveBuffer, 0, bytesRead); } bytesLeftToReceive -= bytesRead / operation; int percentage = Convert.ToInt32((((double)nbo - (double)bytesLeftToReceive) / (double)nbo) * (double)100); progressDialogue.SetProgress(percentage); progressDialogue.ChangeText("Downloading...\n" + (((double)nbo - (double)bytesLeftToReceive) / 1000000).ToString("f2") + " / " + ((double)nbo / 1000000).ToString("f2") + "Mo"); //receivedMsg.AddRange(bytes); } progressDialogue.CloseForm(); receiveBuffer = null; Stream.Close(); //check checksum Console.WriteLine(checksum); if (this.checksum != "null" && useChecksum) { if (!this.checksum.Equals(getChecksum(path))) { mainForm.ShowError("the received file is not the same as the sending one"); } } receiveFinished = true; }