廢話不多說,直接上代碼
接收端:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
public class RecivePhoto : MonoBehaviour
{
private Socket m_socket;
private IPEndPoint m_ipEp;
private Thread m_thread;
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("D:/HoloLive/01.png");
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[1024 * 256];
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"), 1200);
m_socket.Bind(m_ipEp);
m_socket.Listen(30);
isRunningThread = true;
m_thread = new Thread(ReciveMeg);
m_thread.Start();
}
void OnDistory()
{
isRunningThread = false;
m_socket.Close();
}
}
發送端:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
public class SendPhoto : MonoBehaviour
{
private Socket socket = null;
private IPEndPoint endPoint = null;
// Use this for initialization
void Start()
{
InitSocketEnv();
SendMegEvent();
}
public void SendMegEvent()
{
SendPhotoMessage("1.png");
}
void SendPhotoMessage(string fileName)
{
//byte[] buffer = ReadImg(fileName); //null
FileStream fs = new FileStream("D:/HoloLive/Photo/" + 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();
}
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"), 1200);
socket.Connect(endPoint);
}
void OnDestory()
{
socket.Close();
}
}
uj5u.com熱心網友回復:
自頂啊,千萬別沉啊uj5u.com熱心網友回復:
剛開始接觸網路通訊這一塊,不知道該怎么解決uj5u.com熱心網友回復:
不會C#看代碼 大概是你發完第一包資料之后直接將TCP連接斷掉了
看你執行了這個 socket.Close();
然后Client那邊 InitSocketEnv()若只執行了一次的話 那么 在發完第一張圖片后,服務器就將tcp連接斷開了
之后client斷開連接,無法發送第二張圖片
解決辦法的話!
1:不斷開TCP連接,采用通信協議來區分兩張圖片的分隔位置
2:在斷開連接之后 Client 重新執行initSocketEnv();從新連接服務端發送第二包資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/75422.html
標籤:網絡通信
