IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Unity UDP传输图片 -> 正文阅读

[网络协议]Unity UDP传输图片

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

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-12-14 16:19:48  更:2021-12-14 16:21:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 6:49:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码