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

2020-08-19 11:18:10 浏览数 (1)

运行结果:

发送端代码:

代码语言:javascript复制
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("发送成功");
        }
    }
}

接收端代码:

代码语言:javascript复制
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));

        }
    }
}

参考链接:

https://blog.csdn.net/yuhijk2055/article/details/87935783

0 人点赞