static private void sendFile(object info) { Dictionary <string, object> callerInfo = (Dictionary <string, object>)info; //wait for server to acknowledge file with its properties ClientMainWindow caller = (ClientMainWindow)callerInfo["caller"]; Byte[] buffer = new byte[64]; int rec; try { rec = client.Receive(buffer); //thread waits here! } catch { caller.disconnected(); return; } if (rec <= 0) { caller.disconnected(); return; } string serverResponse = Encoding.Default.GetString(buffer); if (serverResponse.Contains("$SENDFILE#")) { string fileName = (string)callerInfo["fileName"]; //start to send the actual file client.SendFile(fileName); waitUploading(callerInfo); } }
static private void waitUploading(Dictionary <string, object> callerInfo) { ClientMainWindow caller = (ClientMainWindow)callerInfo["caller"]; string path = (string)callerInfo["fileName"]; FileInfo info = new FileInfo(path); string filename = info.Name; //wait for confirmation of upload. Byte[] buffer = new byte[64]; int rec; try { rec = client.Receive(buffer); //thread waits here! } catch { caller.disconnected(); return; } if (rec <= 0) { caller.disconnected(); return; } string serverResponse = Encoding.Default.GetString(buffer); Dictionary <string, string> responseStatus = JsonConvert.DeserializeObject <Dictionary <string, string> >(serverResponse); if (responseStatus["UploadStatus"] == "OK") { caller.Invoke((MethodInvoker)(() => caller.writeLogEvent(filename + " is uploaded!"))); MessageBox.Show("Upload completed!", "Upload Progress", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void sendNewNameButton_Click(object sender, EventArgs e) { Dictionary <String, String> fileInfo = new Dictionary <string, string>(); fileInfo.Add("fileProcess", "RENAME"); fileInfo.Add("newFileName", newNameTextField.Text); fileInfo.Add("oldFileName", oldFileName); SendMessage(JsonConvert.SerializeObject(fileInfo)); Byte[] buffer = new byte[32768]; int rec; try { rec = client.Receive(buffer); //thread waits here! } catch { caller.disconnected(); return; } if (rec <= 0) { caller.disconnected(); return; } string serverResponse = Encoding.Default.GetString(buffer); if (serverResponse.Contains("ERROR")) { caller.writeLogEvent("A file with the name " + newNameTextField.Text + " already exist. Cannot rename."); MessageBox.Show("A file with the name " + newNameTextField.Text + " already exist. Cannot rename.", "Rename Problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else if (serverResponse.Contains("SUCCESS")) { caller.writeLogEvent(oldFileName + " renamed to " + newNameTextField.Text); this.Close(); } }
public static void downloadTheFile(Stream toWrite, File fileInfo, Socket n, ClientMainWindow caller) { //same approach with uploading a file but now the client is the recipient. Dictionary <string, string> request = new Dictionary <string, string>(); request.Add("fileProcess", "DOWNLOAD"); request.Add("fileName", fileInfo.fileName); SendMessage(n, JsonConvert.SerializeObject(request)); Byte[] buffer = new byte[32768]; int rec = n.Receive(buffer); //thread waits here! if (rec <= 0) { caller.disconnected(); return; } string newmessage = Encoding.Default.GetString(buffer); newmessage = newmessage.Substring(0, newmessage.IndexOf("\0")); File comingFile; if (newmessage.Contains("\"fileSize\":")) { comingFile = JsonConvert.DeserializeObject <File>(newmessage); Console.Write("started to get file:\r\n"); Byte[] fileBuffer = new byte[comingFile.fileSize]; SendMessage(n, "$SENDFILE#"); int readBytes = 0; int minBlockSize = fileBlockSize < comingFile.fileSize ? fileBlockSize : (int)comingFile.fileSize; readBytes += n.Receive(fileBuffer, 0, minBlockSize, SocketFlags.None); while (readBytes != comingFile.fileSize) { if ((comingFile.fileSize - readBytes) < minBlockSize) { int diff = (int)comingFile.fileSize - readBytes; readBytes += n.Receive(fileBuffer, readBytes, diff, SocketFlags.None); } else { readBytes += n.Receive(fileBuffer, readBytes, minBlockSize, SocketFlags.None); } } toWrite.Write(fileBuffer, 0, (int)comingFile.fileSize); toWrite.Close(); caller.Invoke((MethodInvoker)(() => caller.writeLogEvent(comingFile.fileName + " is downloaded!"))); MessageBox.Show("Download is completed!", "Download Progress", MessageBoxButtons.OK, MessageBoxIcon.Information); } }