Unity Socket UdpClient

UdpClient

using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Text;
using SwanEngine.Events;
using UICore;

/// <summary>
/// udp客户端
/// </summary>
public class UDPClient : MonoBehaviour
{
    public string ip = "127.0.0.1";
    private int port = 6000;

    void Awake()
    {

    }

    /// <summary>
    /// UDP发送消息
    /// </summary>
    /// <param name="str"></param>
    /// <param name="ip"></param>
    /// <param name="port"></param>
    public void UDPSends(string str)
    {
        byte[] byteSendArray = new byte[100];
        byteSendArray = Encoding.UTF8.GetBytes(str);

        IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ip), port);
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        EndPoint ep = (EndPoint)ipe;

        socket.SendTo(byteSendArray, ep);
        socket.Close();
        Debug.Log("send");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容