public void listClient() { List <DTOInfor> myList = BUSInfor.getListInfor(); foreach (DTOInfor list in myList) { dtgvClient.Rows.Add(list.FileName, list.Size.ToString(), list.IpClient, list.PortClient.ToString(), list.FromClient, "Finish", list.Size.ToString(), list.Flag); i_indexFile++; } }
public void downloadFile() { try { //Kiểm tra path chứa file : if (Str_sourse == null) { Str_sourse = me.myPathSave; } if (Str_sourse.EndsWith("\\") == false) { Str_sourse = Str_sourse + "\\"; } //kết nối đến Client share file : S_download = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int i_Port = System.Convert.ToInt32(Port_connect, 10); S_download.Connect(IP_connect, i_Port); //Gửi tên file cho Client để download : //Download@FileName@null. String namefile = "Download@C:\\Share\\" + this.Str_nameFile + "@null"; byte[] byData = System.Text.Encoding.ASCII.GetBytes(namefile); this.S_download.Send(byData); //Nhận thông tin báo hiệu cho phép download : "Accept" hoặc "Close" byte[] accept = new byte[10]; int i_accept = this.S_download.Receive(accept); char[] chars_accept = new char[i_accept]; System.Text.Decoder d_accept = System.Text.Encoding.UTF8.GetDecoder(); int charLen_accept = d_accept.GetChars(accept, 0, i_accept, chars_accept, 0); String C_accept = new System.String(chars_accept); //Client share file báo hiệu không tồn tại file và đóng kết nối: if (C_accept == "Close") { MessageBox.Show("You Can't Download File \" " + this.Str_nameFile + " \" because Client has not file !", "MessageBox Download", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); S_download.Disconnect(true); S_download.Close(); Thread.CurrentThread.Interrupt(); return; } //Client share file báo hiệu cho phép download : if (C_accept == "Accept") { Str_sourse += Str_nameFile; //Bắt đầu Download dữ liệu : this.result = ReceiveFile(Str_sourse, S_download); if (C_online == false) { //Gửi tín hiệu kết thúc download offline lên server : String StrEnd = "endOffline@Finish@null"; byte[] byStrEnd = System.Text.Encoding.ASCII.GetBytes(StrEnd); S_server.Send(byStrEnd); //Thông báo người dùng biết đã download offline xong : MessageBox.Show("Download offline file \"" + this.Str_nameFile + "\" is successful!", "MessageBox Download", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); S_download.Close(); Thread.CurrentThread.Interrupt(); return; } //Gửi tín hiệu kết thúc download lên server : //Ending : Download @Finish @fileName @username @null. String StrEn = "Download@Finish@" + Str_nameFile + "@" + UserName + "@null"; byte[] byStrEn = System.Text.Encoding.ASCII.GetBytes(StrEn); S_server.Send(byStrEn); //Cap nhat du lieu cho file Access : DataInfor.Flag = true; DataInfor.Size = (int)sizeDownload; BUSInfor.Update(DataInfor); //Cap nhat len giao dien DataGridView : //===========Mien Gang======================= S_index.WaitOne(); me.checkDown(i_index, Str_nameFile, UserName, sizeDownload, stateDown); S_index.Release(); //===========Mien Gang======================= //Thông báo người dùng biết đã download xong : if (result == true) { MessageBox.Show("Downloading file \"" + this.Str_nameFile + "\" is successful!", "MessageBox Download", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); } //ngắt kết nối Client share file : S_download.Close(); Thread.CurrentThread.Abort(); return; } } catch (SocketException se) { MessageBox.Show(se.Message); } }
private void ButClearAll_Click(object sender, EventArgs e) { dtgvClient.Rows.Clear(); i_indexFile = 0; BUSInfor.DeleteAll(); }
//==============================================Listenning to Download :======================================================================================================== //==============================================Connection to Download :======================================================================================================== private void button2_Click_1(object sender, EventArgs e) { try { if (BoxIP.Text == "" || BoxPort.Text == "") { MessageBox.Show("Input : IP and Port.", "MessageBox Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); ButDownload.Enabled = true; return; } if (BoxSaveFile.Text == "" || BoxFileName.Text == "") { MessageBox.Show("Input : Name File and Save File.", "MessageBox Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); return; } if (BoxSaveFile.Text == "C:\\") { MessageBox.Show("Can't Save file in Folder C because it's limitted by Admin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DirectoryInfo testFolder = new DirectoryInfo(BoxSaveFile.Text); if (testFolder.Exists == false) { if (MessageBox.Show("Computer has not folder :" + "\"" + BoxSaveFile.Text + "\". \n" + "Do you want to create folder with this name ?", "MessageBox Error ", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { testFolder.Create(); } else { return; } } FileInfo testFileExit; myPathSave = BoxSaveFile.Text; if (BoxSaveFile.Text.EndsWith("\\") == false) { testFileExit = new FileInfo(BoxSaveFile.Text + "\\" + BoxFileName.Text); } else { testFileExit = new FileInfo(BoxSaveFile.Text + BoxFileName.Text); } if (testFileExit.Exists == true) { if (MessageBox.Show("You have file on computer with name" + "\"" + BoxSaveFile.Text + "\\" + BoxFileName.Text + "\". \n" + "Do you want to download again this file ?", "MessageBox Error ", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } else { try { testFileExit.Delete(); } catch (IOException) { //MessageBox.Show(se.Message); } } } int r = this.dtgvSearch.CurrentCell.RowIndex; infor.FileName = dtgvSearch.Rows[r].Cells[0].Value.ToString(); infor.Size = Convert.ToInt32(dtgvSearch.Rows[r].Cells[1].Value); infor.IpClient = dtgvSearch.Rows[r].Cells[2].Value.ToString(); infor.PortClient = Convert.ToInt32(dtgvSearch.Rows[r].Cells[3].Value); infor.FromClient = dtgvSearch.Rows[r].Cells[4].Value.ToString(); if (dtgvSearch.Rows[r].Cells[5].Value.ToString() == "False") { if (MessageBox.Show("Do you want download the file ? \n Because Client share file offline", "MessageBox Stop Server", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } else { //Send String : //Offline @nameFile @size @Username @IPofMe @PortofMe @null. byte[] Waiting = new byte[1024]; String Str_Waiting = "Offline@" + dtgvSearch.Rows[r].Cells[0].Value.ToString() + "@" + dtgvSearch.Rows[r].Cells[1].Value.ToString() + "@" + dtgvSearch.Rows[r].Cells[4].Value.ToString() + "@" + textBox9.Text + "@" + textBox8.Text + "@null"; byte[] byWaiting = System.Text.Encoding.ASCII.GetBytes(Str_Waiting); this.S_loginConnect.Send(byWaiting); } return; } //Send information to server : //Download @nameFile @size @Username @Me @null byte[] byDownload = new byte[1024]; String Str_download = "Download@" + dtgvSearch.Rows[r].Cells[0].Value.ToString() + "@" + dtgvSearch.Rows[r].Cells[1].Value.ToString() + "@" + dtgvSearch.Rows[r].Cells[4].Value.ToString() + "@" + UserName + "@null"; byDownload = System.Text.Encoding.ASCII.GetBytes(Str_download); this.S_loginConnect.Send(byDownload); //Receive String "Start" from Server : int i_Start = this.S_loginConnect.Receive(byDownload); char[] chars_Start = new char[i_Start]; System.Text.Decoder d_Start = System.Text.Encoding.UTF8.GetDecoder(); int charLenStart = d_Start.GetChars(byDownload, 0, i_Start, chars_Start, 0); System.String S_Start = new System.String(chars_Start); if (S_Start == "Start") { MessageBox.Show("Begin Download File : " + dtgvSearch.Rows[r].Cells[0].Value.ToString(), "Download File ", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); //begin create thread to download : Thread T_clientDownload = new Thread(new ThreadStart(connectClient)); T_clientDownload.Start(); //Add Thread vao List danh sach cac Thread Download : T_clientDownload.Name = infor.FileName + "@" + infor.FromClient + "@" + i_indexFile.ToString(); L_threadDownload.Add(T_clientDownload); listClientLoad(); //phoi hop xu ly : doi thread T_clientDownload chay truoc. Sema_addClient.WaitOne(); } else { infor.Flag = false; listClientWait(); i_indexFile++; MessageBox.Show("Please, Waiting to Download File : " + dtgvSearch.Rows[r].Cells[0].Value.ToString(), "Waiting Download", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); } BUSInfor.Insert(infor); ButDownload.Enabled = true; } catch (SocketException) { ButDownload.Enabled = true; } }