网站匹配服务器,Unity局域网匹配服务器

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值