当前位置 博文首页 > unity工具人的博客:tcp发送图片流

    unity工具人的博客:tcp发送图片流

    作者:[db:作者] 时间:2021-07-20 09:33

    发送端:

    using UnityEngine;
    using System.Collections;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System;
    
    public class SendPhoto : MonoBehaviour
    {
    
        private Socket socket = null;
    
        private IPEndPoint endPoint = null;
    
        // Use this for initialization
        void Start()
        {
            InitSocketEnv();       
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.S))
            {
                SendMegEvent();
            }
        }
    
        public void SendMegEvent()
        {
            SendPhotoMessage("Texture003.jpg");
        }
    
        void SendPhotoMessage(string fileName)
        {
            //byte[] buffer = ReadImg(fileName); //null
    
            FileStream fs = new FileStream(Application.streamingAssetsPath + "/" + fileName, FileMode.OpenOrCreate, FileAccess.Read);
            BinaryReader strread = new BinaryReader(fs);
            byte[] byt = new byte[fs.Length];
            strread.Read(byt, 0, byt.Length - 1);
    
            //byte[] size = new byte[4];
            //size = BitConverter.GetBytes(byt.Length);
    
            socket.Send(byt);
            //socket.Send(size);
            print("已发送!");
    
            fs.Close();
            socket.Close();
        }
    
        byte[] ReadImg(string fileName)
        {
            FileInfo fileInfo = new FileInfo(Application.dataPath + "/" + fileName);
            byte[] buffer = new byte[fileInfo.Length];
            using (FileStream fs = fileInfo.OpenRead())
            {
                fs.Read(buffer, 0, buffer.Length);
            }
    
            return buffer;
        }
    
        void InitSocketEnv()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8696);
            socket.Connect(endPoint);
        }
    
        void OnDestory()
        {
            socket.Close();
        }
    }
    

    接收端:

    using UnityEngine;
    using System.Collections;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;
    using System.Collections.Generic;
    using System;
    
    public class RecivePhoto : MonoBehaviour
    {
        private Socket m_socket = null;
        private IPEndPoint m_ipEp = null;
    
        private Thread m_thread = null;
        private bool isRunningThread = false;
    
        private Queue<byte[]> m_queue;
    
        // Use this for initialization
        void Start()
        {
            m_queue = new Queue<byte[]>();
            InitSocketEnv();
        }
    
        // Update is called once per frame
        void Update()
        {
            if (m_queue.Count > 0)
            {
                Debug.Log(m_queue.Count);
    
                byte[] temp = m_queue.Dequeue();
    
                FileStream fs = File.Create(Application.dataPath + "/" + "2.jpg");
                fs.Write(temp, 0, temp.Length);
                fs.Close();
            }
    
    
        }
    
        void ReciveMeg()
        {
            while (isRunningThread)
            {
                Socket socket = m_socket.Accept();
    
                //获取图片字节流长度
                //byte[] dataSize = new byte[4];
                //int rect = socket.Receive(dataSize, 0, 4, SocketFlags.None);
                //int size = BitConverter.ToInt32(dataSize, 0);
    
                //Debug.Log(size);
    
                byte[] buffer = new byte[2000000];
                socket.Receive(buffer, buffer.Length, SocketFlags.None);
    
                //byte[] bufferSize = new byte[4];
                //socket.Receive(bufferSize, 0, 4, SocketFlags.None);
                //int size = BitConverter.ToInt32(bufferSize, 0);
                //Debug.Log(size);
    
                m_queue.Enqueue(buffer);
    
                socket.Close();
            }
    
            Debug.Log("stop");
        }
    
        void InitSocketEnv()
        {
            m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_ipEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8696);
            m_socket.Bind(m_ipEp);
            m_socket.Listen(5);
    
            isRunningThread = true;
            m_thread = new Thread(ReciveMeg);
            m_thread.Start();
        }
    
        void OnDistory()
        {
            isRunningThread = false;
            m_socket.Close();
        }
    }
    

    小插曲:
    使用中出现了一些问题,就是只能发送一次,再发就报错了去掉下图中的Close也不行
    在这里插入图片描述
    耽误了好久
    后来放进协程里这样写终于能用了
    在这里插入图片描述

    cs