Unity3D TCP图片传输

参考 https://blog.csdn.net/qq_34818497/article/details/79464675
发送端代码如下:

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();
        SendMegEvent();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void SendMegEvent()
    {
        SendPhotoMessage("haha.jpg");
    }

    void SendPhotoMessage(string fileName)
    {
        //byte[] buffer = ReadImg(fileName); //null

        FileStream fs = new FileStream(Application.dataPath+"/" + 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);

        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();
    }
}

你可能感兴趣的:(Unity3D TCP图片传输)