Roane 2022-08-08 19:01 采纳率: 25%
浏览 65
已结题

在unity里面 怎么得到接收udp消息的消息头

在unity里面 怎么得到接收udp消息的消息头
初始化
ipEnd = new IPEndPoint(IPAddress.Parse(ipAddress), ConnectPort);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ipEnd);
//定义客户端
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
clientEnd = (EndPoint)sender;
//开启一个线程连接
connectThread = new Thread(new ThreadStart(SocketReceive));
connectThread.Start();
接收 recvData = new byte[81920];
recvLen = socket.ReceiveFrom(recvData, ref clientEnd);
recvStr = Encoding.UTF8.GetString(recvData, 0, recvLen);

  • 写回答

3条回答 默认 最新

  • ilmss 2022-08-08 19:31
    关注
    获得1.20元问题酬金
    
    using System.Collections.Generic;
     using System.Net;
     using System.Net.Sockets;
     using System.Text;
     using System.Threading;
     using UnityEngine;
     using System;
     
     public class UDPClient2Client : MonoBehaviour
     {
         public static UDPClient2Client Instance = null;
         private UdpClient client;
         private Thread thread = null;
         private IPEndPoint remotePoint;
         public string ip="127.0.0.1";
         private int port = 9090;
     
         public Action<string> receiveMsg = null;
     
         private string receiveString = null;     void Awake()
         {
             if (Instance == null)
             {
                 Instance = this;
                 DontDestroyOnLoad(gameObject);
             }
             else
             {
                 Destroy(gameObject);
             }
         }
         // Use this for initialization
         void Start()
         {
     
             
     
           //  ip = IPManager.ipAddress;
             remotePoint = new IPEndPoint(IPAddress.Any, 0);
             thread = new Thread(ReceiveMsg);
             thread.Start();
         }
         //接受消息
         void ReceiveMsg()
         {
             while (true)
             {
                 client = new UdpClient(port);
     
                 byte[] receiveData = client.Receive(ref remotePoint);//接收数据
                 receiveString = Encoding.UTF8.GetString(receiveData);
     
                 client.Close();            
             }
         }
         //发送消息
         void SendMsg(IPAddress ip, string _msg)
         {
             IPEndPoint remotePoint = new IPEndPoint(ip, port);//实例化一个远程端点 
     
             if (_msg != null)
             {
                 byte[] sendData = Encoding.Default.GetBytes(_msg);
                 UdpClient client = new UdpClient();
                 client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点 
                 client.Close();//关闭连接
             }
         }
         // Update is called once per frame
         void Update()
         {
             if (!string.IsNullOrEmpty(receiveString))
             {
                 
                 //消息处理
                 if (receiveMsg != null && remotePoint.Address.ToString() != ip)
                 {
                     Debug.Log(remotePoint.Address + ":" + remotePoint.Port + " ---> " + receiveString);
                     receiveMsg.Invoke(receiveString);
                     receiveString = null;
                     
                 }
             }
             
        }
         void OnDestroy()
         {
             SocketQuit();
        }
         void SocketQuit()
         {
             thread.Abort();
             thread.Interrupt();
             client.Close();
         }
         void OnApplicationQuit()
         {
             SocketQuit();
         }
     
         
      
     }
      接受消息添加委托   
    
    void Start()
        {
            
    
            UDPClient2Client.Instance.receiveMsg = Rstring;
    
    
    
    }
      void Rstring(string str) {
            Debug.Log(str);
          }
    
    评论

报告相同问题?

问题事件

  • 系统已结题 8月16日
  • 创建了问题 8月8日

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法