Unity3D Socket通信 UDP

本文详细探讨了如何在Unity3D游戏引擎中使用UDP协议进行Socket通信,涵盖了创建UDP客户端和服务器,数据包发送与接收,以及网络稳定性优化等关键点,为实时多人游戏和其他网络应用提供了基础架构。
摘要由CSDN通过智能技术生成

图1
这里写图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//使用Socket引用的命名空间
using System.Net.Sockets;
//绑定端口号引用的命名空间
using System.Net;
//打開線程引用的命名空間
using System.Threading;

public class UDPSocket
{
    Socket mySocket;
    /// <summary>
    /// 接收的数据的数组
    /// </summary>
    byte[] ReciveDataBytes;
    /// <summary>
    /// 声明线程
    /// </summary>
    Thread reciveThread;
    /// <summary>
    /// 是否开启线程
    /// </summary>
    private bool IsThreading;
    /// <summary>
    /// 声明有个代理
    /// </summary>
    /// <param name="buffer">接收到的数据</param>
    /// <param name="ReciveLength">接收到的数据的数组长度</param>
    /// <param name="port">端口号</param>
    /// <param name="ip">IP地址</param>
    public delegate void ReciveDelegate(byte[] buffer, int ReciveLength, int port, string ip);
    /// <summary>
    /// 声明一个代理
    /// </summary>
    ReciveDelegate reciveDelegate;
    /// <summary>
    /// 构造方法重写
    /// </summary>
    /// <param name="temReciveDelegate">代理方法</param>
    /// <param name="port">端口号</param>
    /// <param name="reciveLength">接收到的数据的长度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值