Conexão de rede UDP do Unity

1. Introdução

        UDP é a abreviatura de User Datagram Protocol. É um protocolo da camada de transporte . Ele não precisa estabelecer uma conexão antes de transmitir os dados. Quando deseja enviar dados, eles são lançados diretamente na rede. Se a extremidade receptora estiver online e a rede estiver limpa neste momento, os dados serão recebidos. Se a extremidade receptora não estiver online ou os dados forem perdidos devido ao congestionamento da rede. A velocidade com que o UDP transmite dados é limitada apenas pela velocidade com que o aplicativo gera dados, a capacidade do computador e a largura de banda de transmissão. Portanto, o UDP é caracterizado pela velocidade de transmissão rápida e pelo risco de perda de pacotes. Geralmente é usado em programas que requerem transmissão em alta velocidade e não são tão seguros para dados (como: telefone pela Internet, chat de vídeo, etc.)

 

Em segundo lugar, o servidor

using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System;
using System.Collections;

namespace Mx.Net
{
    /// <summary>UDP通信服务器</summary>
    public class UdpServer : MonoBehaviour
    {
        private Socket m_Socket;
        private EndPoint m_EndPoint;
        private IPEndPoint m_IpEnd;
        private byte[] m_RecvData = new byte[1024];
        private byte[] m_SendData = new byte[1024];
        private Thread m_Thread;
        private Queue m_MsgQueue;
        public Action<byte[]> onReceiveEvent = null;

        private void Awake()
        {
            m_MsgQueue = new Queue();
        }

        private void OnDestroy()
        {
            Close();
        }

        private void Update()
        {
            //由于我们接受数据是在多线程上,所以接受到的数据我们需要返回到主线程使用
            if (m_MsgQueue.Count != 0)
            {
                OnReceiveEvent((byte[])m_MsgQueue.Dequeue());
            }
        }

        /// <summary>开启UPD服务器</summary>
        public void OpenServer(int port)
        {
            m_IpEnd = new IPEndPoint(IPAddress.Any, port);
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            m_Socket.Bind(m_IpEnd);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            m_EndPoint = (EndPoint)sender;
            m_Thread = new Thread(new ThreadStart(SocketReceive));
            m_Thread.Start();
        }

        /// <summary>发送消息</summary>
        public void SendMsg(string msg)
        {
            if (m_Socket == null||string.IsNullOrEmpty(msg)) return;

            m_SendData = new byte[1024];
            m_SendData = Encoding.ASCII.GetBytes(msg);
            m_Socket.SendTo(m_SendData, m_SendData.Length, SocketFlags.None, m_EndPoint);
        }

        /// <summary>发送Bytes数据</summary>
        public void SendBytes(byte[] data)
        {
            if (m_Socket == null|| data==null) return;

            m_Socket.SendTo(data, data.Length, SocketFlags.None, m_EndPoint);
        }

        /// <summary>接收消息事件</summary>
        private void OnReceiveEvent(byte[] data)
        {
            if (onReceiveEvent != null) onReceiveEvent(data);

            string recvStr = Encoding.ASCII.GetString(data);
            Debug.Log(GetType() + "/Receive()/接收到的消息:" + recvStr);
        }

        /// <summary>接收消息</summary>
        private void SocketReceive()
        {
            while (true)
            {
                m_RecvData = new byte[1024];
                int length = m_Socket.ReceiveFrom(m_RecvData, ref m_EndPoint);
                byte[] data = new byte[length];
                Array.Copy(m_RecvData, data, length);
                m_MsgQueue.Enqueue(data);
            }
        }

        /// <summary>关闭连接</summary>
        private void Close()
        {
            if (m_Thread != null)
            {
                m_Thread.Interrupt();
                m_Thread.Abort();
            }

            if (m_Socket != null) m_Socket.Close();
        }

    }
}

 

Tres, o cliente

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;

namespace Mx.Net
{
    /// <summary>UDP通信客户端</summary>
    public  class UdpClient : MonoBehaviour
    {
        private Socket m_Socket;
        private EndPoint m_EndPoint;
        private IPEndPoint m_IpEnd;
        private byte[] m_RecvData = new byte[1024];
        private byte[] m_SendData = new byte[1024];
        private Thread m_Thread;
        private Queue m_MsgQueue;
        public Action<byte[]> onReceiveEvent = null;

        private void Awake()
        {
            m_MsgQueue = new Queue();
        }

        private void OnDestroy()
        {
            Close();
        }

        private void Update()
        {
            if (m_MsgQueue.Count != 0)
            {
                OnReceiveEvent((byte[])m_MsgQueue.Dequeue());
            }
        }

        /// <summary>开启客户端</summary>
        public void OpenClient(int port)
        {
            m_IpEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            m_EndPoint = (EndPoint)sender;

            m_Thread = new Thread(new ThreadStart(SocketReceive));
            m_Thread.Start();
        }

        /// <summary>发送消息</summary>
        public void SendMsg(string msg)
        {
            if (m_Socket == null) return;

            m_SendData = new byte[1024];
            m_SendData = Encoding.UTF8.GetBytes(msg);
            m_Socket.SendTo(m_SendData, m_SendData.Length, SocketFlags.None, m_IpEnd);
        }

        /// <summary>发送Bytes数据</summary>
        public void SendBytes(byte[] data)
        {
            if (m_Socket == null) return;

            m_Socket.SendTo(data, data.Length, SocketFlags.None, m_IpEnd);
        }

        /// <summary>接收消息</summary>
        private void SocketReceive()
        {
            while (true)
            {
                m_RecvData = new byte[1024];
                int length = m_Socket.ReceiveFrom(m_RecvData, ref m_EndPoint);
                byte[] data = new byte[length];
                Array.Copy(m_RecvData, data, length);
                m_MsgQueue.Enqueue(data);
            }
        }

        /// <summary>接收消息事件</summary>
        private void OnReceiveEvent(byte[] data)
        {
            if (onReceiveEvent != null) onReceiveEvent(data);

            string recvStr = Encoding.ASCII.GetString(data);
            Debug.Log(GetType() + "/OnReceiveEvent()/接收到的消息:" + recvStr);
        }

        /// <summary>关闭客户端</summary>
        private void Close()
        {
            if (m_Thread != null)
            {
                m_Thread.Interrupt();
                m_Thread.Abort();
            }

            if (m_Socket != null) m_Socket.Close();
        }
    }
}

 

Quatro, caso de sala de chat

-------------------- Todo será atualizado mais tarde --------------------

 

Grupo de intercâmbio Unity QQ: 299412191 Dê as boas-vindas a alunos interessados ​​em participar do Unity.

 

Acho que você gosta

Origin blog.csdn.net/a451319296/article/details/109693690
Recomendado
Clasificación