在实现unity联机的过程中,想要实现客户端能够自动发现局域网中的服务器
开了17个线程,否则要20+min才能扫描完全部网络
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
public class test : MonoBehaviour
{
[SerializeField]
private string My_Ip = "";
[SerializeField]
private string ip_section = "127.0.0."; // + 0~255 默认网段 本机网段
[SerializeField]
private List<string> ipList = new List<string>(); //搜索到的全部ip
Thread t1;
Thread t2;
Thread t3;
Thread t4;
Thread t5;
Thread t6;
Thread t7;
Thread t8;
Thread t9;
Thread t10;
Thread t11;
Thread t12;
Thread t13;
Thread t14;
Thread t15;
Thread t16;
Thread t17;
void Start()
{
My_Ip = GetIp();
ip_section = My_Ip.Remove(My_Ip.LastIndexOf('.')) + '.'; // 修改搜索网段
Debug.Log("My ip address : " + My_Ip);
//多线程
t1 = new Thread(T1);
t2 = new Thread(T2);
t3 = new Thread(T3);
t4 = new Thread(T4);
t5 = new Thread(T5);
t6 = new Thread(T6);
t7 = new Thread(T7);
t8 = new Thread(T8);
t9 = new Thread(T9);
t10 = new Thread(T10);
t11 = new Thread(T11);
t12 = new Thread(T12);
t13 = new Thread(T13);
t14 = new Thread(T14);
t15 = new Thread(T15);
t16 = new Thread(T16);
t17 = new Thread(T17);
}
public void OnSearchIpButtonClick()
{
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
t6.Start();
t7.Start();
t8.Start();
t9.Start();
t10.Start();
t11.Start();
t12.Start();
t13.Start();
t14.Start();
t15.Start();
t16.Start();
t17.Start();
}
// Update is called once per frame
void Update()
{
}
void T1()
{
for (int i = 1; i <= 15; i++)
{
System.Net.NetworkInformation.Ping myPing = new System.Net.NetworkInformation.Ping(); //Ping(ip_section + i)