关于网络的数据传输我就是个小白,所以今天学习一下简易的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(); } } }
运行结果:
开启服务:
发送图片:
局域网测试传输图片通过,希望对大家学习有帮助,如有错误可以联系我哦。