C# Socket 簡易的圖片傳輸

來源:互聯網
上載者:User

標籤: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 簡易的圖片傳輸

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.