UnityでUDPサーバーを実装する方法

Unityのスタンドアロンアプリケーション(Windows/Mac)でUDPサーバーを実装する方法を記載します。UDPプロトコルを使用することによって、PC間・アプリケーション間でデータ送受信し連携が容易となります。

使用した環境

  • Macbook Pro (13-inch, 2017, Four Thunderbolt 3Ports)
  • macOS Mojave バージョン 10.14.4
  • Unity 2019.2.8f1 Personal

UDPサーバークラスのソースコード

UDPサーバー機能を実装するにあたり、下記プログラムを実装しました。

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

public class UDPServer
{
    /// <summary>
    /// デリゲート 受信時イベント
    /// </summary>
    /// <param name="strMsg"></param>
    public delegate void ReceivedHandler(string strMsg);

    /// <summary>
    /// 受信時イベント
    /// </summary>
    public ReceivedHandler Received;

    /// <summary>
    /// 受信処理 スレッド
    /// </summary>
    private Thread thread;

    /// <summary>
    /// リッスンポート
    /// </summary>
    private int nListenPort;

    /// <summary>
    /// UDPクライアント
    /// </summary>
    private UdpClient client;

    //--------------------------------------------------------------------------

    /// <summary>
    /// UDPServer
    /// </summary>
    public UDPServer(int port = 20000)
    {
        nListenPort = port;
        client = null;
    }

    /// <summary>
    /// UDP受信 リッスン開始
    /// </summary>
    public void ListenStart()
    {
        client = new UdpClient(nListenPort);
        thread = new Thread(new ThreadStart(Thread));
        thread.Start();
        Debug.Log("UDP Receive thread start");
    }

    /// <summary>
    /// 解放処理
    /// </summary>
    public void Dispose()
    {
        if(thread != null)
        {
            thread.Abort();
            thread = null;
        }

        if(client != null)
        {
            client.Close();
            client.Dispose();
            client = null;
        }
    }

    /// <summary>
    /// スレッド
    /// </summary>
    private void Thread()
    {
        while (true)
        {
            if (client != null)
            {
                try
                {
                    IPEndPoint ep = null;
                    byte[] rcvBytes = client.Receive(ref ep);

                    string rcvMsg = string.Empty;
                    rcvMsg = System.Text.Encoding.UTF8.GetString(rcvBytes);
                    if (rcvMsg != string.Empty)
                    {
                        Debug.Log("UDP受信メッセージ : " + rcvMsg);
                        Received?.Invoke(rcvMsg);
                    }
                }
                catch (System.Exception e)
                {
                    Debug.Log(e.Message);
                }

            }
            else
            {
                Debug.Log("Error:client = null");
            }
        }
    }
}

UDPサーバークラスの使い方

前述のUDPServerクラスは下記のように記述することで使用できます。

UDPServer udpServer;

void Start()
{
    udpServer = new UDPServer();
    udpServer.Received += ReceiveUdp;
    udpServer.ListenStart();
}

private void ReceiveUdp(string strMsg)
{
    // UDPメッセージを受信したらこの関数が呼ばれる
    Debug.Log(strMsg);
}

よく読まれている記事

コメント

タイトルとURLをコピーしました