Socket编程(Unity简单案例,UDP协议)

服务器端

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

public class UDPServer : MonoBehaviour
{
    public int port;

    Socket socketServer;
    IPEndPoint iPEnd;
    EndPoint clientEnd;
    Thread connectT;
    byte[] sendData = new byte[1024];
    byte[] receiveData = new byte[1024];

	void Start ()
    {
        InitSocket();
	}
    void InitSocket()
    {
        iPEnd = new IPEndPoint(IPAddress.Any, port);
        socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        socketServer.Bind(iPEnd);

        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        clientEnd = (EndPoint)sender;

        connectT = new Thread(SocketReceive);
        connectT.Start();
    }

    private void SocketSend(string msg)
    {
        sendData = new byte[1024];
        sendData = Encoding.UTF8.GetBytes(msg);
        socketServer.SendTo(sendData,sendData.Length, SocketFlags.None, clientEnd);
    }
    private void SocketReceive()
    {
        while (true)
        {
            receiveData = new byte[1024];
            int length = socketServer.ReceiveFrom(receiveData, ref clientEnd);
            Debug.Log("Client Form " + clientEnd.ToString());

            string message = Encoding.UTF8.GetString(receiveData, 0, length);
            Debug.Log(message);
        }
    }
    void OnDestroy()
    {
        if (connectT != null)
        {
            connectT.Interrupt();
            connectT.Abort();
        }

        if (socketServer != null)
            socketServer.Close();
    }
}

客户端

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

public class UDPClient : MonoBehaviour
{
    public int port;
    public string ip;

    private Socket socketClient;
    private EndPoint serverEnd;
    private IPEndPoint iPEnd;
    private Thread connectT;
    private byte[] sendData = new byte[1024];
    private byte[] receiveData = new byte[1024];

    private void Start()
    {
        InitSocket();
    }
    void InitSocket()
    {
        iPEnd = new IPEndPoint(IPAddress.Parse(ip), port);
        socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        serverEnd = (EndPoint)sender;
        SocketSend("Hello!");
        
        connectT = new Thread(SocketReceiver);
        connectT.Start();
    }
    private void SocketSend(string msg)
    {
        sendData = new byte[1024];
        sendData = Encoding.UTF8.GetBytes(msg);
        socketClient.SendTo(sendData, 0, sendData.Length, SocketFlags.None,iPEnd);
    }
    private void SocketReceiver()
    {
        while (true)
        {
            receiveData = new byte[1024];
            int length = socketClient.ReceiveFrom(receiveData, ref serverEnd);
            Debug.Log("message form:" + serverEnd.ToString());
            string message = Encoding.UTF8.GetString(receiveData, 0, length);
            Debug.Log(message);
        }
    }
    void OnDestroy()
    {
        if (connectT != null)
        {
            connectT.Interrupt();
            connectT.Abort();
        }

        if (socketClient != null)
            socketClient.Close();

    }
}


你可能感兴趣的:(Unity,网络,UDP,网络编程)