Unity UDP network connection

1. Introduction

        UDP is the abbreviation of User Datagram Protocol. It is a transport layer protocol. It does not need to establish a connection before transmitting data. When it wants to send data, it is directly thrown on the network. If the receiving end is online and the network is clear at this time, the data will be received. If the receiving end is offline or the data is lost due to network congestion. The speed at which UDP transmits data is only limited by the speed at which the application generates data, the capacity of the computer, and the transmission bandwidth. Therefore, UDP is characterized by fast transmission speed and the risk of packet loss. It is generally used in programs that require high-speed transmission and are not so safe for data (such as: Internet phone, video chat, etc.)

 

Second, the server

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

    }
}

 

Three, the client

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

 

Four, chat room case

--------------------Todo will update later--------------------

 

Unity QQ exchange group: 299412191 Welcome students who are interested in Unity to join.

 

Guess you like

Origin blog.csdn.net/a451319296/article/details/109693690