/// <summary> /// Send Tex Data To EndPoint /// </summary> /// <param name="i">index of Client Socket in server_requests List </param> public void send_Text_Data(int i) { if ((server_requests[i].Myscoket != null) && (server_requests[i].Myscoket.Connected)) { try { server1 = server_requests[i]; server1.Writer = new BinaryWriter(server1.MynetworkStream); if (TextBox_Input.Text != " ") { server1.Writer.Write((byte)0); //Sending Text Data server1.Writer.Write("Server>>" + TextBox_Input.Text); } } catch (Exception Err) { } //MessageBox.Show(Err.Message); } if (TextBox_Input.Text.ToLower() == "terminate") // Optional ; this Way Can be used To Disconnect The Connection { server1.Myscoket.Close(); } } else { //MessageBox.Show("Reomote End point is Abort The Connecton"); //TextBox_Input.Clear(); } }
/// <summary> /// Send Any File to The Endpoint /// </summary> /// <param name="i">index of Client Socket in server_requests List </param> public void send_file(int i) { if ((server_requests[i].Myscoket != null) && (server_requests[i].Myscoket.Connected)) { try { server1 = server_requests[i]; server1.Writer = new BinaryWriter(server1.MynetworkStream); server1.Writer.Write((byte)1); // Sending Any File byte[] Buffer = File.ReadAllBytes(path); server1.Writer.Write(Buffer.Length); //send the size of file // Writing Data server1.MynetworkStream.Write(Buffer, 0, Buffer.Length); TextBox_Send.Text = Buffer.Length.ToString(); } catch (Exception error) { }//MessageBox.Show(error.Message); } } else { //MessageBox.Show("Client is Disconnected"); } }
//Save Button ; Very Important ; Click at ShowConnected Client To See the Sizes Files that Server is Received #region private void button_Save_Click(object sender, EventArgs e) private void button_Save_Click(object sender, EventArgs e) { FileStream file = null; if ((!(checkBox_Send2All.Checked)) && (checkedListBox_Clients.SelectedItems.Count == 0)) { MessageBox.Show("Pleas Select The Client You Want To Save File form it ", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (checkBox_Send2All.Checked) //Send To All Conncected Client { for (int i = 0; i < server_requests.Count; i++) { SaveFileDialog.Title = "Saving The Received File From " + Convert.ToString(i); if ((server_requests[i] != null) && (server_requests[i].Myscoket.Connected)) { server1 = server_requests[i]; if (SaveFileDialog.ShowDialog() == DialogResult.OK) { // You need to Try-catch-finally try { if (server1.ArrFile != null) { file = new FileStream(SaveFileDialog.FileName, FileMode.Create);; file.Write(server1.ArrFile, 0, server1.ArrFile.Length); file.Flush(); MessageBox.Show("The Received File From " + "Client " + i.ToString() + "is Saved ", "Confirm The Save Operation ", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("the Client " + i.ToString() + " Does Not Send Any File to the Server "); } } catch (Exception Error) { MessageBox.Show(Error.Message); } finally { try { file.Close(); } catch (Exception err) { } } } } } //End For } //End Else else // Send To more Clients { for (int i = 0; i < checkedListBox_Clients.CheckedItems.Count; i++) { SaveFileDialog.Title = "Saving The Received File From " + Convert.ToString(checkedListBox_Clients.CheckedItems[i]); string s = Convert.ToString(checkedListBox_Clients.CheckedItems[i]).Remove(0, 6); int clientnumber = Convert.ToInt32(s); server1 = server_requests[clientnumber]; if (SaveFileDialog.ShowDialog() == DialogResult.OK) { // You need to Try-catch-finally try { if (server1.ArrFile != null) { file = new FileStream(SaveFileDialog.FileName, FileMode.Create);; file.Write(server1.ArrFile, 0, server1.ArrFile.Length); file.Flush(); MessageBox.Show("The Received File From " + Convert.ToString(checkedListBox_Clients.CheckedItems[i]) + "is Saved ", "Confirm The Save Operation ", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("the Client " + clientnumber.ToString() + " Does Not Send Any File to the Server "); } } catch (Exception Error) { MessageBox.Show(Error.Message); } finally { try { file.Close(); } catch (Exception err) { } } } } //End For } //end Else }