打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# Socket 简易的图片传输

  关于网络的数据传输我就是个小白,所以今天学习一下简易的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();        }    }}

运行结果:

开启服务:

发送图片:

 局域网测试传输图片通过,希望对大家学习有帮助,如有错误可以联系我哦。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#+AO中调用ArcToolbox中自定义的模型
VA18.2 数据流概述
vb.net发邮件源码
如何实现VB.NET 打印控件的使用
使用MSMQ传送文字、类、图片
C# Winform 窗体美化(八、Icon)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服