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