在Unity3D上进行UDP通讯

在Unity3D上进行UDP通讯

  • 简介
  • 代码

简介

Unity3D版本: 2018.3.0f2 Personal
Visual Studio版本:2017

在Unity3D上监听指定端口接收UDP数据
指定本地端口与目标端口发送UDP数据

代码

创建文件TCPUDPSocket.cs

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

public class TCPUDPSocket : MonoBehaviour
{

    UdpClient udpClient;
    IPEndPoint targetPoint;




    bool udp_send_flag = false;
    bool udp_recv_flag = false;
    public void Start_UDPSender(int self_port,int target_port)
    {
        if (udp_send_flag == true)
            return;
        udpClient = new UdpClient(self_port);
        
        targetPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), target_port);
       
        udp_send_flag = true;
    }
    Thread thrRecv;
    private UdpClient UDPrecv;
    private IPEndPoint endpoint;
    private byte[] recvBuf;
    private Thread recvThread;
    public void Start_UDPReceive(int recv_port)
    {
        if (udp_recv_flag == true)
            return;

        UDPrecv = new UdpClient(new IPEndPoint(IPAddress.Any, recv_port));
        endpoint = new IPEndPoint(IPAddress.Any, 0);
        recvThread = new Thread(new ThreadStart(RecvThread));
        recvThread.IsBackground = true;
        recvThread.Start();

        udp_recv_flag = true;


    }


    public void Close_UDPSender()
    {
        if (udp_send_flag == false)
            return;
        udpClient.Close();
        udp_send_flag = false;
    }

    public void Close_UDPReceive()
    {
        if (udp_recv_flag == false)
            return;
        thrRecv.Interrupt();
        thrRecv.Abort();
        udp_recv_flag = false;
    }

    public void Write_UDPSender(string strdata)
    {
        if (udp_send_flag == false)
            return;
        byte[] sendData = Encoding.Default.GetBytes(strdata);
        udpClient.Send(sendData, sendData.Length, targetPoint);
    }


    public string Read_UDPReceive()
    {
        returnstr = String.Copy(recvdata);
        if (old)
        {
            old = false;
            recvdata = "";
            return returnstr;
        }
        else
            return "";
    }
    bool old = false;
    string returnstr;
    string recvdata;
    private bool messageReceive;
    private void ReceiveCallback(IAsyncResult ar)
    {
        recvBuf = UDPrecv.EndReceive(ar, ref endpoint);
        recvdata += Encoding.Default.GetString(recvBuf);
        old = true;
        messageReceive = true;
    }
    private void RecvThread()
    {
        messageReceive = true;

        while (true)
        {
            try
            {
                if (messageReceive)
                {
                    UDPrecv.BeginReceive(new AsyncCallback(ReceiveCallback), null);
                    messageReceive = false;
                }
            }
            catch(Exception e)
            {

            }
        }
    }





    GUIStyle bb = new GUIStyle();


    // Start is called before the first frame update
    void Start()
    {
        bb.normal.background = null;
        bb.normal.textColor = new Color(1, 0, 0);
        bb.fontSize = 40;
        //Start_TCPClient("192.168.199.231",2333);
        Start_UDPSender(7777,6666);
        Start_UDPReceive(5555);
    }


    string aaa;
    // Update is called once per frame
    void Update()
    {
        aaa = Read_UDPReceive();
    }

    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 200, 200), aaa, bb);
    }
}

其中包含一些测试代码,可以删除

Start_UDPSender(7777,6666);
Start_UDPReceive(5555);

第一个函数的入口参数:指定本地发送端口,指定对方接收端口
第二个函数的入口参数:指定本地接收端口

你可能感兴趣的:(有关Unity3D的一些东西,有关网络通讯的一些东西)