using System; 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 { private static Socket sock; private static IPEndPoint iep1; private static byte[] data; private Thread t; string Error_Message; public int udpPort = 9050; private void Start() { BroadcastIP(); } public void BroadcastIP() { sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); iep1 = new IPEndPoint(IPAddress.Broadcast, udpPort); data = Encoding.ASCII.GetBytes(SystemInfo.deviceName); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); t = new Thread(BroadcastMessage); t.Start(); } private void BroadcastMessage() { while (true) { sock.SendTo(data, iep1); Thread.Sleep(2000); } } private void OnDestroy() { sock.Close(); } }