不使用socket进行
unity下的
-
- private Thread connectThread;
- private UdpClient udpClient;
-
- public Queue LogQueue = new Queue();
-
- public static UDPManager Instance{get;private set;}
-
- private void Awake()
- {
- Instance = this;
- }
-
- private void Start()
- {
- udpClient = new UdpClient();
- udpClient.EnableBroadcast = true;
-
- connectThread = new Thread(new ThreadStart(SocketReceive));
- Thread.Sleep(1000);
- connectThread.Start();
- }
-
-
- private void Update()
- {
- SendMessanger();
- }
-
- void SocketReceive()
- {
- UdpClient udpClient = new UdpClient(6900);
- try
- {
- while (true)
- {
- IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- while (true)
- {
- byte[] recvData = udpClient.Receive(ref remoteIpEndPoint);
- string recvStr = Encoding.UTF8.GetString(recvData);
- LogQueue.Enqueue(recvStr);
- }
- }
-
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- udpClient.Close();
- }
-
- }
-
- void SendMessanger()
- {
- if (LogQueue.Count <= 0) return;
- string strArray = LogQueue.Dequeue() as string;
- if (strArray!=null)
- {
- Debug.Log(strArray);
- }
- }
-
- public void SendAllStart(string content)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(content);
- udpClient.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Loopback, 6910));
- }
c#其他下使用:
- public partial class NetSever
- {
- private static NetSever instance;
-
- public static NetSever Instance
- {
- get
- {
- if (instance == null)
- {
- instance = new NetSever();
- }
- return instance;
- }
- }
-
-
-
- static UdpClient udpClient;
- static int connectPort;
- public static Queue LogQueue = new Queue();
-
-
-
- static IPAddress remoteIP = IPAddress.Parse("127.0.0.1");
- IPEndPoint remotePoint = new IPEndPoint(remoteIP, connectPort);//实例化一个远程端点
-
- //网络端口唤醒
- public void Init()
- {
-
- //初始化协议
- if (GetIP() == "")
- {
- Console.WriteLine ("当前IP地址不存在,或没有信号进行连接!");
- return;
- }
- udpClient = new UdpClient();
- udpClient.EnableBroadcast = true;
- connectPort = 6900;
- //添加事件订阅信息
- AddEventNeight();
-
- 开启一个线程
-
- Task task = new Task(SocketReceive);
- task.Start();
-
-
-
- }
-
-
- private void AddEventNeight()
- {
-
- }
-
- string GetIP()
- {
- //本机名
- string hostName = Dns.GetHostName();
- //会返回所有地址,包括IPv4和IPv6
- IPAddress[] addressList = Dns.GetHostAddresses(hostName);
- foreach (IPAddress ip in addressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- return ip.ToString();
- }
- }
- return "127.0.0.1";
- }
-
-
- static void SocketReceive()
- {
-
- UdpClient client = null;
- string receiveString = null;
-
- //实例化一个远程端点,IP和端口可以随意指定,等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点
- IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
-
- //NetSever.Instance.SendAllStart("网络开启");
-
- UdpClient udpClient = new UdpClient(6910);
- try
- {
- while (true)
- {
- IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- while (true)
- {
- byte[] recvData = udpClient.Receive(ref remoteIpEndPoint);
- receiveString = Encoding.UTF8.GetString(recvData);
- string[] date = null;
- if (receiveString.Contains("\r\n"))
- {
- receiveString = receiveString.TrimEnd((char[])"\r\n".ToCharArray());
- }
- if (receiveString.Contains("010"))
- {
- date = receiveString.Split('-');
- receiveString = "010";
- }
- if (receiveString.Contains("openFile"))
- {
- date = receiveString.Split('-');
- receiveString = "openFile";
- }
-
- switch (receiveString)
- {
- case "Text":
- Console.WriteLine("网络端口测试!!!已收到信息");
- NetSever.Instance.SendAllStart("网络已打开");
- break;
-
- }
-
- }
- }
- }
- catch (Exception)
- {
- //NetSever.Instance.SendAllStart("网络已打开");
- throw;
- }
- finally
- {
- udpClient.Close();
- }
-
- }
-
-
- //发送端口
- public void SendAllStart(string content)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(content);
-
- UdpClient udpClient = new UdpClient();
- try
- {
- udpClient.Connect("127.0.0.1", 6900);
-
- udpClient.Send(bytes, bytes.Length);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
-
-
-
- }
至于为啥这么写,只能说经验之谈