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

Unity UDP传输图片相关推荐

  1. UDP传输图片(分包)

    前提:需要了解QUdp的简单通信,比如收发个字符串 QPixmap图片类,以此类来加载图片 QBuffer和QByteArray来记录数据 memcpy函数的用法 分包概念:举个例子就是客户端(C)给 ...

  2. Unity局域网传输图片等文件

    使用Unity实现局域网下传输各种文件,这里我传输图片为例,放上关键代码: 客户端代码: using System; using System.Collections.Generic; using S ...

  3. unity socket传输图片_python3实现socket传输图片

    我最近在做一个项目的时候 需要把树莓派上的摄像头拍摄的图片实时传输到我的PC上 我想通过socket完成这个功能 我找了找网上的代码,好多都是python2.x版本的,或者是图片总是传不过来的,只能自 ...

  4. unity udp广播 android,unity发送局域网广播信息

    开发中有事须要一个功能是教师机经过广播发送给学生机实现通信,这时能够考试使用socket的udp广播功能来实现,代码以下:socket using System; using System.Text; ...

  5. python Intel Realsense udp协议 局域网传输实时视频流并通过窗口显示 (opencv压缩解码)

    文章目录 发送端 接收端 执行结果 发送端 接收端 发送端 # -*- coding: utf-8 -*- """ @File : 200103_obstacle_det ...

  6. Unity 语音通话功能

    一.录制语音 Unity自带Api public RecognizeVoice(){string[] microPhoneName = Microphone.devices;if(microPhone ...

  7. Unity 接入Facebook

    最近由于项目需要,接入Fackbook.原先的已经有登录模块了,不过是shaderSDK的,考虑到国内这些插件会对数据动手脚,只有换掉了. 回到正题,接入的过程还是遇到不少问题,先整理下我的资料,以下 ...

  8. 安卓版的java程序代码

    正确例题 import java.util.*; public class Ha{ public static void main(String[] args) { String a[]={" ...

  9. 7.3 Qt图形程序设计 【C++】

    一.Qt的介绍 Qt是一个跨平台的C++图形用户界面(GUI)的应用程序框架. 父类也叫基类 子类也叫派生类 二.Qt的框架 main.cpp文件 #include "mywidget.h& ...

最新文章

  1. linux服务器部署laravel出现putenv() has been disabled for security reasons
  2. OpenGL6-纹理动画
  3. spring_01概念及案例
  4. DRD:线程错误检测器
  5. 【ElasticSearch】Es 源码之 NodeConnectionsService 源码解读
  6. 一些有关。NET界面处理与多线程的文章
  7. Flex 页面跳转 四种方法
  8. 计算机报名填错学制,2017年在职研究生填报信息发现错误怎么修改?
  9. android mm 修改路径,Android 编译系统模块
  10. 人机大战!人工智能轻松打败美国空军
  11. 还在烦恼ToF的误差问题?有人帮你找到解决方法了!
  12. Matlab似然比检验函数,似然比检验 LRT
  13. Gradle同步工程下载依赖慢
  14. OAI搭建编译eNB报错
  15. 女生适合从事什么工作?程序员!
  16. php5 imap,LIV. IMAP, POP3 and NNTP Functions - PHP 5 中文文档
  17. 云管平台可以应用于哪些行业?传统行业可以吗?
  18. Java——【习题】java泛型练习题
  19. 北京注册公司还是注册个体户有区别
  20. 文件下载时直接对流进行zip加密压缩

热门文章

  1. C#.NET开源项目、机器学习、商务智能
  2. Ultra Wideband Wireless Communication
  3. 苹果系统下载工具Mac Downloader
  4. 如何有效实现软件的需求管理 - 4
  5. linux查看磁盘硬件日志,Linux下如何查看硬件信息
  6. 国外优秀 Flex 网站源码模板与实例
  7. 西门子编程软件,V16还没上手,最新版本V17已经出来了!
  8. 应用决策树算法进行股票财务特征分析
  9. 51单片机蜂鸣器的使用
  10. Azure | AZ-204 认证之旅-风起