Unity UDP 통신

3681 단어 UnityUDP
Unity UDP 통신 UDP의 특징: 언제든지 연결을 끊고 연결합니다.(클라이언트와 서버의 차이는 없음) 서버
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;

public class UDPServer : MonoBehaviour {
    IPEndPoint ServerIpEndPoint;
    IPEndPoint ClientIpEndPoint;
    Socket socket;
    Thread serverThread;
    // Use this for initialization
    void Start () {
        Init();

    }
	
	// Update is called once per frame
	void Update () {
        
	}
    void Init() {
        // ip 
        ServerIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),11000);
        // 
        ClientIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11001);
        // 
        socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
        // ip 
        socket.Bind(ServerIpEndPoint);
        // 
        serverThread = new Thread(Recv);
        serverThread.Start();
    }
    public void Recv() {
        // 
        EndPoint endPoint = new IPEndPoint(IPAddress.Any,0);
        while (true)
        {
            byte[] recvData = new byte[1024];
            int recvLen = socket.ReceiveFrom(recvData, ref endPoint);
            string str = Encoding.UTF8.GetString(recvData, 0,recvLen);
            print(str);
        }
    }
    public void Send(string str) {
        byte[] sendData = new byte[1024];
        sendData = Encoding.UTF8.GetBytes(str);
        // 
        socket.SendTo(sendData,sendData.Length, SocketFlags.None, ClientIpEndPoint);
    }
    private void OnApplicationQuit()
    {
        // , 
        if (socket!=null)
        {
            socket.Close();
        }
        if (serverThread!=null)
        {
            serverThread.Interrupt();
            serverThread.Abort();
        }
    }
}


클라이언트
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;

public class UDPClient : MonoBehaviour
{
    IPEndPoint ClientIpEndPoint;
    IPEndPoint ServerIpEndPoint;
    Socket socket;
    Thread serverThread;
    // Use this for initialization
    void Start()
    {
        Init();
    }

    // Update is called once per frame
    void Update()
    {

    }
    void Init()
    {
        // IP 
        ClientIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11001);
        // 
        ServerIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
        // 
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        // ip 
        socket.Bind(ClientIpEndPoint);
        // 
        serverThread = new Thread(Recv);
        serverThread.Start();
    }
    public void Recv()
    {
        EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
        while (true)
        {
            byte[] recvData = new byte[1024];
            int recvLen = socket.ReceiveFrom(recvData, ref endPoint);
            string str = Encoding.UTF8.GetString(recvData, 0, recvLen);
            print(str);
        }
    }
    public void Send(string str)
    {
        byte[] sendData = new byte[1024];
        sendData = Encoding.UTF8.GetBytes(str);
        socket.SendTo(sendData, sendData.Length, SocketFlags.None, ServerIpEndPoint);
    }
    private void OnApplicationQuit()
    {
        // , 
        if (socket != null)
        {
            socket.Close();
        }
        if (serverThread != null)
        {
            serverThread.Interrupt();
            serverThread.Abort();
        }
    }
}

좋은 웹페이지 즐겨찾기