Unity多线程Ping(1~255)

该博客介绍了在Unity中如何利用C#实现多线程Ping技术,以快速扫描局域网内的服务器,通过开启17个线程大大缩短了原本20多分钟的扫描时间。
摘要由CSDN通过智能技术生成

在实现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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值