欢迎光临
我的个人博客网站

C# Socket TCP发送图片与接收图片

如果需要查看更多文章,请微信搜索公众号 csharp编程大,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

———————————————————————————————————————————

运行结果:

C# Socket TCP发送图片与接收图片

 

 发送端代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;  using System.IO; using System.Net.Sockets; using System.Net;  namespace tcp {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void Form1_Load(object sender, EventArgs e)         {             //textBox1.Text = "101.91.224.228";             textBox1.Text = "192.168.1.100";             textBox2.Text = "8008";         }         FileStream fs = new FileStream(@"1.jpg", FileMode.Open);         private void SendImage(IPAddress remoteIP,int Port)         {             //实例化socket                            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);             IPEndPoint ipep = new IPEndPoint(remoteIP, Port);             socket.Connect(ipep);             long contentLength = fs.Length;             //第一次发送数据包的大小                        socket.Send(BitConverter.GetBytes(contentLength));             while (true)            {                 //每次发送128字节                                byte[] bits = new byte[128];                 int r = fs.Read(bits, 0, bits.Length);                 if (r <= 0) break;                 socket.Send(bits, r, SocketFlags.None);             }             socket.Close();             fs.Position = 0;         }          private void button1_Click(object sender, EventArgs e)         {             SendImage(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text));             MessageBox.Show("发送成功");         }     } } 

  接收端代码:

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; using System.Threading.Tasks; using System.Windows.Forms;  namespace recTCP {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void Form1_Load(object sender, EventArgs e)         {              //RecviveImage();             textBox1.Text = "8008";         }          private void RecviveImage(int port)         {             new Thread(delegate () {             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);             IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);             socket.Bind(ipep);             socket.Listen(2);             while (true)                 {                     try                     {                         byte[] data = new byte[8];                         Socket clientSocket = socket.Accept();                         if (clientSocket.Connected)                         {                             clientSocket.Receive(data, data.Length, SocketFlags.None);                             long contentLength = BitConverter.ToInt64(data, 0);                             int size = 0;                             MemoryStream ms = new MemoryStream();                             while (size < contentLength)                             {                                 byte[] bits = new byte[128];                                 int r = clientSocket.Receive(bits, bits.Length, SocketFlags.None);                                 if (r <= 0) break;                                 ms.Write(bits, 0, r);                                 size += r;                             }                             Image img = Image.FromStream(ms);                             this.Invoke((EventHandler)delegate                              {                                 pictureBox1.Image = img; //更新在窗体控件上                                                        });                             clientSocket.Close();                             ms.Flush();                             ms.Close();                             ms.Dispose();                         }                     }                     catch { }                 }             })             { IsBackground = true}.Start();         }          private void button1_Click(object sender, EventArgs e)         {             RecviveImage(int.Parse(textBox1.Text));          }     } } 

  - – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

赞(0) 打赏
未经允许不得转载:张拓的天空 » C# Socket TCP发送图片与接收图片
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏