Unity Socket UdpClient

UdpClient

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

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

    void Awake()
    {

    }

    /// 
    /// UDP发送消息
    /// 
    /// 
    /// 
    /// 
    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");
    }
}

你可能感兴趣的:(Unity Socket UdpClient)