關於網絡的數據傳輸我就是個小白,所以今天學習一下簡易的Socket圖片傳輸。
客戶端和服務器的連接咱們上次已經學過了,咱們先從簡易的文件傳輸入手。下面開始代碼分析了。
Server.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Server { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lab_pro.Text = "接收:0/100"; } /// <summary> /// 開啟服務 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { button1.Text = "監聽中..."; button1.Enabled = false; Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 121); //設置接收數據緩沖區的大小 byte[] b = new byte[4096]; receiveSocket.Bind(hostIpEndPoint); //監聽 receiveSocket.Listen(2); //接受客戶端連接 Socket hostSocket = receiveSocket.Accept(); //如何確定該數組大小 MemoryStream fs = new MemoryStream(); int length = 0; //每次只能讀取小於等於緩沖區的大小 while ((length = hostSocket.Receive(b)) > 0) { fs.Write(b, 0, length); if (progressBar1.Value <100) { progressBar1.Value++; lab_pro.Text = "接收:" + progressBar1.Value + "/100"; } } progressBar1.Value = 100; lab_pro.Text = "接收:" + progressBar1.Value + "/100"; fs.Flush(); Bitmap Img = new Bitmap(fs); Img.Save(@"reveive.jpg", ImageFormat.Png); //關閉寫文件流 fs.Close(); //關閉接收數據的Socket hostSocket.Shutdown(SocketShutdown.Receive); hostSocket.Close(); //關閉發送連接 receiveSocket.Close(); } } }
客戶端Client.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Client { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); OpenFileDialog openFileDialog1 = new OpenFileDialog(); Byte[] imgByte = new byte[1024]; /// <summary> /// 打開本地文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btm_scane_Click(object sender, EventArgs e) { this.openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG" +"|All Files (*.*)|*.*"; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { try { string path = this.openFileDialog1.FileName; lab_path.Text = path; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); imgByte = new Byte[fs.Length]; fs.Read(imgByte, 0, imgByte.Length); fs.Close(); } catch (Exception) { } } } /// <summary> /// 向服務器發送數據 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_send_Click(object sender, EventArgs e) { //實例化socket IPEndPoint ipendpiont = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 121); sendsocket.Connect(ipendpiont); MessageBox.Show("服務器IP:"+sendsocket.RemoteEndPoint); sendsocket.Send(imgByte); sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send); sendsocket.Close(); sendsocket.Dispose(); } } }
運行結果:
開啟服務:
發送圖片:
局域網測試傳輸圖片通過,希望對大家學習有幫助,如有錯誤可以聯系我哦。