using UnityEngine;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine.UI;
public class BroadCast : MonoBehaviour
{
private Button btnServer;
private Button btnClient;
private Button btnExit;
private Text txt;
private string strInfo;
private int port = 6677;
const string specialText = "123!!@@##$$%%-MessageFormServerBroadCast";
bool serverIsRun = false;
bool clientIsRun = false;
Thread serverThread = null;
UdpClient UdpSend = null;
Thread clientThread = null;
UdpClient UdpListen = null;
float flowedime = 0;
void StartServer()//服务器一直发消息
{
strInfo = "Starting Server...";
if (serverThread != null && serverThread.IsAlive) return;
serverThread = new Thread(()=>
{
UdpSend = new UdpClient();
while (serverIsRun)
{
Thread.Sleep(500);
byte[] buf = Encoding.Unicode.GetBytes(specialText);
UdpSend.Send(buf, buf.Length, new IPEndPoint(IPAddress.Broadcast, port));
}
UdpSend.Close();
});
serverThread.IsBackground = true;
InitServer();
serverThread.Start();
strInfo = "Server Started";
}
void StartClient()//客户端收消息直到收到服务器的IP
{
strInfo = "Starting Client...";
if (clientThread != null && clientThread.IsAlive) return;
clientThread = new Thread(()=>
{
UdpListen = new UdpClient(new IPEndPoint(IPAddress.Any, port));
while (clientIsRun)
{
Thread.Sleep(10);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);
byte[] bufRev = UdpListen.Receive(ref endpoint);//this method will block, Close() can stop it
string msg = Encoding.Unicode.GetString(bufRev, 0, bufRev.Length);
if (msg.Contains(specialText))
{
strInfo = "I'm client, receive from ip:" + endpoint.Address.ToString();
UdpListen.Close();
return;
}
else
{
strInfo = "I'm client, receive from ip:" + endpoint.Address.ToString() + ",but it no use!";
}
}
UdpListen.Close();
});
clientThread.IsBackground = true;
InitClient();
clientThread.Start();
strInfo = "Receiving...";
}
void Start()
{
txt = GameObject.Find("TextInfo").GetComponent();
btnServer = GameObject.Find("ButtonServer").GetComponent();
btnClient = GameObject.Find("ButtonClient").GetComponent();
btnExit = GameObject.Find("ButtonExit").GetComponent();
btnServer.onClick.AddListener(StartServer);
btnClient.onClick.AddListener(StartClient);
btnExit.onClick.AddListener(ExitGame);
}
void ExitGame()
{
StopServer();
StopClient();
Application.Quit();
}
void FixedUpdate()
{
CheckListenTime();
}
void CheckListenTime()
{
if (clientIsRun)
{
flowedime += Time.deltaTime;
if (flowedime > 10f)
{
strInfo = "Timeout! Maxtime=10";
StopClient();
}
}
}
void InitServer()
{
serverIsRun = true;
if (UdpSend != null) UdpSend.Close();
}
void InitClient()
{
clientIsRun = true;
flowedime = 0f;
if (UdpListen != null) UdpListen.Close();
}
void StopServer()
{
serverIsRun = false;
if (UdpSend != null) UdpSend.Close();
if (serverThread != null && serverThread.IsAlive) serverThread.Abort();
}
void StopClient()
{
clientIsRun = false;
if (UdpListen != null) UdpListen.Close();
if (clientThread != null && clientThread.IsAlive) clientThread.Abort();
}
void Update()
{
if (txt.text != strInfo) txt.text = strInfo;
}
}