標籤:des style blog io ar color os sp for
關於網路的資料轉送我就是個小白,所以今天學習一下簡易的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(); } }}
運行結果:
開啟服務:
發送圖片:
區域網路測試傳輸圖片通過,希望對大家學習有協助,如有錯誤可以聯絡我哦。
C# Socket 簡易的圖片傳輸