Server:
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using System;
public class UDPServer
{
private Socket server;
/// <summary>
/// 接收缓冲区数据
/// </summary>
private byte[] data;
public Action<byte[]> OnMesageCallback;
public UDPServer(string ip, int port)
{
data = new byte[1024 * 1024*10];
server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
ThreadPool.QueueUserWorkItem(ReceiveMessage);
}
public void DisConnect()
{
server?.Shutdown(SocketShutdown.Both);
server?.Dispose();
}
private void ReceiveMessage(object obj)
{
while (true)
{
int len = server.Receive(data);
if(len>0)
{
byte[] rec = new byte[len];
Buffer.BlockCopy(data,0, rec, 0, len);
OnMesageCallback?.Invoke(rec);
}
}
}
public void SendMessage(string content, IPEndPoint endPoint)
{
byte[] sed = Encoding.UTF8.GetBytes(content);
server.SendTo(sed, endPoint);
}
}
ServerManager:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class ServerManager : MonoBehaviour
{
public GameObject singnature;
public Transform itemParent;
private UDPServer server;
public string ip = "10.19.201.92";
private byte[] recData;
private bool isReceived;
// Start is called before the first frame update
void Start()
{
server = new UDPServer(ip, 7777);
server.OnMesageCallback += OnMessageCallBack;
}
void OnDestroy()
{
server?.DisConnect();
}
private void OnMessageCallBack(byte[] rec)
{
recData = rec;
isReceived = true;
}
private void Update()
{
if(isReceived)
{
isReceived = false;
Texture2D tex = new Texture2D(500, 500);
tex.LoadImage(recData);
GameObject item = GameObject.Instantiate(singnature, itemParent);
item.GetComponent<RawImage>().texture = tex;
}
}
}
Client:
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using System;
public class UDPClient
{
private Socket server;
/// <summary>
/// 接收缓冲区数据
/// </summary>
private byte[] data;
public Action<byte[]> OnMesageCallback;
public UDPClient()
{
data = new byte[1024 * 1024*10];
server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
ThreadPool.QueueUserWorkItem(ReceiveMessage);
}
public void SendMessage(byte[] sed, IPEndPoint endPoint)
{
server.SendTo(sed, endPoint);
}
private void ReceiveMessage(object obj)
{
while (true)
{
int len = server.Receive(data);
if (len > 0)
{
byte[] rec = new byte[len];
Buffer.BlockCopy(data, 0, rec, 0, len);
OnMesageCallback?.Invoke(rec);
}
}
}
public void SendMessage(string content, IPEndPoint endPoint)
{
byte[] sed = Encoding.UTF8.GetBytes(content);
server.SendTo(sed, endPoint);
}
}
UDPManager
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class UDPManager : MonoBehaviour
{
public static UDPManager instance;
private UDPClient client;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
client = new UDPClient();
client.OnMesageCallback += OnMessageCallback;
}
private void OnMessageCallback(byte[] rec)
{
string result = Encoding.UTF8.GetString(rec);
Debug.Log(result);
}
public void Send(string content)
{
client.SendMessage(content, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.19.201.92"), 7777));
}
public void Send(byte[] content)
{
client.SendMessage(content, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.19.201.92"), 7777));
}
}