微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity UDP通讯

UDPSend:发送端脚本,Ip为接收端本地Ip, port为自定义int类型(但是要与接收端port一致)
UDPReceive:接收端脚本,port为自定义int类型(但是要与发送端port一致)

using UnityEngine;
using System.Net;
using System.Net.sockets;
using System.Text;
public class UDPSend : MonoBehavIoUr
{  
    private string ip;   //主机ip地址
    private int port;
    private IPAddress ipAddress;
    private IPEndPoint endPoint;
    private Socket socket;
    private EndPoint server;
    private byte[] sendData;                //发送内容,转化为byte字节
    void Awake()
    {
        ip = "192.168.1.100";
        port = 8899;
    }

    //发送函数
    public void  Send(float value)             //参数不是字符串时转化为string
    {
        string msg = value.ToString("0");      //传递的值转化为string       
        ipAddress = IPAddress.Parse(ip);            //ip地址
        endPoint = new IPEndPoint(ipAddress, port);  //自定义端口号
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 112);
        server = (EndPoint)sender;
        sendData = new byte[1024];                  //定义发送字节大小
        sendData = Encoding.Default.GetBytes(msg);  //对msg编码
        socket.SendTo(sendData, sendData.Length, SocketFlags.None, endPoint);   //发送信息
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            Send(12);
        }
    }
}

using UnityEngine;
using System.Net;
using System.Net.sockets;
using System.Threading;
using System.Text;
public class UDPReceive : MonoBehavIoUr
{
   
    private IPEndPoint ipEndPoint;
    private Socket socket;
    private Thread thread;
    private byte[] bytes;           //接收到的字节
    private int bytesLength;        //长度
    public string receiveMsg = "";   //接收到的信息
    public bool isOver;
    private int port; //端口号要与发送端一致
    void Start()
    {
        Init();
    }
    //初始化
    private void Init()
    {
        ipEndPoint = new IPEndPoint(IPAddress.Any, port);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        socket.Bind(ipEndPoint);
        thread = new Thread(new ThreadStart(Receive));      //开启一个线程,接收发送端的消息
        thread.IsBackground = true;
        thread.Start();
    }

    //接收消息函数
    private void Receive()
    {
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint remote = (EndPoint)sender;

        while (true)
        {
            bytes = new byte[1024];
            bytesLength = socket.ReceiveFrom(bytes, ref remote);
            receiveMsg = Encoding.ASCII.GetString(bytes, 0, bytesLength);
        }
    }
    //实时接收消息
    void Update()
    {
        if (receiveMsg != "")
        {
            Debug.Log(receiveMsg);
            receiveMsg = "";
        }
    }
    //关闭socket,关闭thread
    private void Ondisable()
    {
        if (socket != null)
        {
            socket.Close();
        }
        if (thread != null)
        {
            thread.Interrupt();
            thread.Abort();
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐