Unity 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();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
photonnetwork.instantiate에서 gamepobject 유형을 생성 한 다음 상태 및 값을 참조하는 방법주로 마지막 기사에서 일어난 일의 수정입니다. 지난번↓ 그럼 주제입니다. (타이틀이 정리되어 없어서 죄송합니다) 우선 전회의 Illegal view ID:0입니다만 photonnetwork.instantiate를 사...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.