Unity利用Socket实现图片的传输

不了解Socket的同学可以参考如下文章:http://blog.csdn.net/linshuhe1/article/details/51386559

发送端代码如下:

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

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

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

        FileStream fs = new FileStream(ConfiInfo.pathPhoto + 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(ConfiInfo.pathPhoto + 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(ConfiInfo.serverIP), ConfiInfo.TcpPort);
        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(ConfiInfo.pathPhoto + "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(ConfiInfo.serverIP), ConfiInfo.TcpPort);
        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();
    }
}

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值