0% found this document useful (0 votes)
42 views

Using Using Using Using Using Using Using Namespace Class Public Static Void New New

The document appears to contain code for creating a network connection in C# to send and receive files between systems. It includes code to open a socket connection, send and receive bytes of data representing file contents and metadata, and save any received files to a specified local folder. The code handles accepting incoming connection requests and receiving files on one system, as well as initiating connections and sending files from another system.

Uploaded by

Le Van Truong Le
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Using Using Using Using Using Using Using Namespace Class Public Static Void New New

The document appears to contain code for creating a network connection in C# to send and receive files between systems. It includes code to open a socket connection, send and receive bytes of data representing file contents and metadata, and save any received files to a specified local folder. The code handles accepting incoming connection requests and receiving files on one system, as well as initiating connections and sending files from another system.

Uploaded by

Le Van Truong Le
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

using using using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Net; System.Net.Sockets; System.IO;

namespace Nhan_dang_chu { class tao_ket_noi { public static void ketnoiserver(String IP) { IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(IP),0); Socket new socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsocket.Connect(endpoint); } public static void guidl(String nguon,String IP,String myIP,int port) { /*try {*/ FileStream stream = new FileStream(nguon, FileMode.Open, FileAccess.ReadWrite); FileInfo info = new FileInfo(nguon); Byte[] mang = new Byte[info.Length]; stream.Read(mang, 0, (int)info.Length); IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(IP), port); // System.Windows.Forms.MessageBox.Show(endpoint.Port.ToString( ));

Socket new socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsocket.Connect(endpoint); Byte[] thongbao1 = new Byte[System.IO.Path.GetFileName(nguon).Length]; String tb1 = System.IO.Path.GetFileName(nguon) + "," + myIP + "," + info.Length.ToString(); tb1 += "," + tb1.Length.ToString() + ","; thongbao1 = Encoding.ASCII.GetBytes(tb1); newsocket.Send(thongbao1, thongbao1.Length, SocketFlags.None); Byte[] doitactraloi1 = new Byte[1]; newsocket.Receive(doitactraloi1, doitactraloi1.Length, SocketFlags.None); String dich1 = Encoding.ASCII.GetString(doitactraloi1); if (dich1 == "y") { newsocket.Send(mang, mang.Length, SocketFlags.None); System.Windows.Forms.MessageBox.Show("Gi d liu thnh cng", "Thng bo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); //newsocket.Close(); } newsocket.Close(); /* } catch (Exception) { System.Windows.Forms.MessageBox.Show("Khng th gi d liu", "Li", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

} */ } } }

using using using using using using using using using using using using using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Windows; System.Windows.Controls; System.Windows.Data; System.Windows.Documents; System.Windows.Input; System.Windows.Media; System.Windows.Media.Imaging; System.Windows.Navigation; System.Windows.Shapes; System.Net; System.Net.Sockets; System.Windows.Forms.Integration; System.IO;

namespace Nhan_dang_chu { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { bool cho; String nguon;

public Window1() { InitializeComponent(); String HostName=Dns.GetHostName(); IPHostEntry entry=Dns.GetHostByName(HostName); foreach(IPAddress ad in entry.AddressList) { textBox1.Text+=ad.ToString(); } nguon = ""; cho = false; Port.Text = "1"; button4.Content = "M kt ni"; } private void button1_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog(); dialog.Filter = @"All File|*.*"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { nguon = dialog.FileName; FileInfo info=new FileInfo(nguon); ten.Text = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName); dinhdang.Text = System.IO.Path.GetExtension(dialog.FileName); kichthuoc.Text = info.Length / 1000 + " Kb"; duongdan.Text = System.IO.Path.GetFullPath(dialog.FileName); } } private void button2_Click(object sender, RoutedEventArgs e) {

tao_ket_noi.guidl(nguon, doitac.Text,textBox1.Text,int.Parse(Port.Text)); } private void button3_Click(object sender, RoutedEventArgs e) { this.Close(); } private void button4_Click(object sender, RoutedEventArgs e) { try { if (button4.Content == "M kt ni") { cho = true; button4.Content = "ng kt ni"; IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(Port.Text)); Socket new socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsocket.Bind(endpoint); newsocket.Listen(10); Socket client = newsocket.Accept(); Byte[] yeucau1 = new Byte[102]; client.Receive(yeucau1, yeucau1.Length, SocketFlags.None); String tb1 = Encoding.ASCII.GetString(yeucau1); String[] tb2 = tb1.Split(','); tb1 = tb1.Substring(0, int.Parse(tb2[3]));

tb2 = tb1.Split(','); if (System.Windows.Forms.MessageBox.Show("Bn c mun nhn tp tin " + tb2[0] + " t a ch " + tb2[1] + "?", "Thng bo", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { String traloi1 = "y"; Byte[] traloi1a = new Byte[1]; traloi1a = Encoding.ASCII.GetBytes(traloi1); client.Send(traloi1a, traloi1a.Length, SocketFlags.None); //client.Close(); String thumucluu = ""; System.Windows.Forms.FolderBrowserDialog dialog2 = new System.Windows.Forms.FolderBrowserDialog(); if (dialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK) { thumucluu = dialog2.SelectedPath + "\\" + tb2[0]; }

//============================================ Byte[] nhanfile = new Byte[int.Parse(tb2[2])]; client.Receive(nhanfile, nhanfile.Length, SocketFlags.None); if (File.Exists(thumucluu) == true) { if (System.Windows.Forms.MessageBox.Show("File ny tn ti.

Bn c mun ghi ln?", "Thng bo", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes) { File.Delete(thumucluu); } else thumucluu += "[1]"; } FileStream stream = new FileStream(thumucluu, FileMode.CreateNew, FileAccess.ReadWrite); stream.Write(nhanfile, 0, (int)nhanfile.Length); MessageBox.Show(" nhn file thnh cng"); } client.Close(); } else if (button4.Content == "ng kt ni") { cho = false; button4.Content = "M kt ni"; } } catch (Exception) { System.Windows.Forms.MessageBox.Show("Khng th nhn d liu", "Looixx", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } } private void Window_Loaded(object sender, RoutedEventArgs e) {

} } }

You might also like