⭐Unity 搭建UDP服务端(02)接收客户端消息

news/2024/5/18 13:00:02 标签: 网络, unity, udp

客户端在上一篇

由于服务器逻辑写的较为简单

所以直接上代码了~

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

public class UdpServer : MonoBehaviour
{
    public static UdpServer instance;
    private void Awake()
    {
        if (instance != null)
        {
            return;
        }
        else
        {
            instance = this;
        }

    }

    public int serverPort = 8080;
    private UdpClient udpServer;

    private void Start()
    {
        udpServer = new UdpClient(serverPort);
        udpServer.BeginReceive(ReceiveCallback, null);
        
    }
    private IPEndPoint Clientip;
    private void ReceiveCallback(IAsyncResult result)
    {
        try
        {

            IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, serverPort);
            byte[] receivedBytes = udpServer.EndReceive(result, ref clientEndPoint);
            string receivedMessage = Encoding.ASCII.GetString(receivedBytes);
            Clientip = clientEndPoint;
            Debug.Log("收到来自客户端的消息: " + receivedMessage + "-----" + clientEndPoint);

            // 继续接收下一个消息
            udpServer.BeginReceive(ReceiveCallback, null);
        }
        catch (Exception e)
        {
            Debug.LogError("Error receiving UDP message: " + e.Message);
        }
    }

    public void SendBroadcastMessage(string message)
    {

        // 发送广播消息
        IPEndPoint endPoint = Clientip;
        byte[] messageBytes = Encoding.ASCII.GetBytes(message);
        udpServer.Send(messageBytes, messageBytes.Length, endPoint);

    }

    private void OnDestroy()
    {
        if (udpServer != null)
        {
            udpServer.Close();
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            Debug.Log("发送");
            SendBroadcastMessage("hello client!");
        }
    }
}


http://www.niftyadmin.cn/n/5246534.html

相关文章

通过异步序列化提高图表性能 Diagramming for WPF

通过异步序列化提高图表性能 2023 年 12 月 6 日 MindFusion.Diagramming for WPF 4.0.0 添加了异步加载和保存文件的功能,从而提高了响应能力。 MindFusion.Diagramming for WPF 提供了一个全面的工具集,用于创建各种图表,包括组织结构图、图…

企业微信小群发公告的接口和功能测试

企业微信小群发公告的接口和功能测试,可以从以下几个方面进行: 功能测试: 1.验证发送公告的基础功能。例如,发送公告是否能够被成功发送到小群中,公告内容是否能够被所有群成员看到。 2.验证公告的多种设置功能&…

Javaweb | Servlet编程

目录: 1.认识Servlet2.编写Servlet3.Servlet的运行机制4.Servlet的生命周期4.1 Servlet生命周期图init()方法doGet()doPost()service()destroy()方法 5.解决“控制台”打印中文乱码问题6.Servlet 和 JSP内置对象 (常用对象)获得out对象获得request 和 response对象获得session对…

Oracle11g RAC无法使用VIP或SCAN IP连接数据库的解决方案

在windows客户端sqlplus工具使用VIP或scan的IP无法连接,报错如下: C:\Users\WJW>sqlplus system/qweasd192.168.2.200:1521/orcl SQL*Plus: Release 11.2.0.1.0 Production on Thu May 17 12:35:28 2012 Copyright (c) 1982, 2010, Oracle. All righ…

python小白入门scanpy,我的学习之路

第一天 :dIntroduction HonKithttps://python.swaroopch.com/ 这本书重点看看数据结构就行了 简单入门 第二天GitHub - huangsam/ultimate-python: Ultimate Python study guide for newcomers and professionals alike.Ultimate Python study guide for newcom…

文件同步及实现简单监控

1. 软件简介 rsync rsync 是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程 数据同步备份的优秀工具。在同步备份数据时,默认情况下,Rsync 通过其 独特的“quick check”算法,它仅同步大小或者最后修改时间发生变化的文 件或…

Rook-ceph(1.12.9最新版)

官网的步骤 git clone --single-branch --branch v1.12.9 https://github.com/rook/rook.git cd rook/deploy/examples kubectl create -f crds.yaml -f common.yaml -f operator.yaml kubectl create -f cluster.yaml整理后的已经替换好的国内镜像的 git clone https://gite…

华清远见嵌入式学习——QT——作业1

作业要求&#xff1a; 代码&#xff1a; ①&#xff1a;头文件 #ifndef LOGIN_H #define LOGIN_H#include <QWidget> #include <QLineEdit> //行编辑器类 #include <QPushButton> //按钮类 #include <QLabel> //标签类 #include <QM…