简体   繁体   English

如何检查数组 <T> 有重复的值?

[英]how to check if array<T> has a duplicated values?

I'm making a small userControl library that searches for active server on a specific port [LAN] . 我正在制作一个小的userControl库,该库在特定端口[LAN]上搜索活动服务器。
I'm using [.Net 4.0] TCP sockets (IPV4) 我正在使用[.Net 4.0] TCP套接字(IPV4)
so first i get the IPAddresses of my computer from : 所以首先我从以下位置获取计算机的IPAddresses:

string sHostName = Dns.GetHostName();
IPHostEntry ipE = Dns.GetHostByName(sHostName);
IPAddress[] IpA = ipE.AddressList;

then i take first 3 parts of the IPAddress so if the input IPAddress = "192.168.1.X" it returns "192.168.1." 那么我会占用IPAddress的前3个部分,因此如果输入IPAddress =“ 192.168.1.X”,它将返回“ 192.168.1”。 using : 使用:

string scanIP = IpA[i].ToString().Substring(0, IpA[i].ToString().LastIndexOf(".")) + ".";

then i search from (ex: 192.168.1. 1 - to 192.168.1. 254 ) 然后我从搜索(例如:192.168.1 1 -至192.168.1 254。)

what i need is to check if scanIP is duplicated to avoid scan 254 IPAddresses more than once.. because the array IpA may returns many IP Addresses (wireless network,wirenetwork,bluetooth network..etc) for ex : my wireless ip is : 192.168.1.25 and my ethernet ip is : 192.168.1.5 我需要检查scanIP是否重复,以避免多次扫描254个IPAddress ..因为阵列IpA可能会返回许多IP地址(无线网络,wirenetwork,蓝牙网络等),例如:我的无线ip是:192.168 .1.25和我的以太网ip是:192.168.1.5

here's my code : 这是我的代码:

  public ServerDiscovery()
    {
        InitializeComponent();
        timer.Elapsed += timer_tick;
    }

    System.Timers.Timer timer = new System.Timers.Timer(5000);
    List<SocketAsyncEventArgs> list = new List<SocketAsyncEventArgs>();

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        listServer.Items.Clear();
        timer.Start();
        //getting array of IP Addresses.
        IPAddress[] IpA = Dns.GetHostByName(Dns.GetHostName()).AddressList;

        for(int j = 0; j < IpA.Length; j++)
        {
            if (IpA[j].AddressFamily == AddressFamily.InterNetwork)  //checking if IP is version 4
            {
                string scanIP = IpA[j].ToString().Substring(0, IpA[j].ToString().LastIndexOf(".")) + ".";
                if (some condition)// i need a condition to ensure that dublicated scanIP doesn't scan again
                {
                    Parallel.For(1, 255, (i, loopState) =>
                        {
                            Scan(scanIP + i);
                        });
                }
            }
        }
    }

    private void Scan(string ipAdd)
    {
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        SocketAsyncEventArgs e = new SocketAsyncEventArgs();
        e.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ipAdd), 9990);
        e.UserToken = s;
        e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);
        list.Add(e);
        s.ConnectAsync(e);
    }

    private void e_Completed(object sender, SocketAsyncEventArgs e)
    {
        if (e.ConnectSocket != null)
        {
            StreamReader sr = new StreamReader(new NetworkStream(e.ConnectSocket));
            ListViewItem item = new ListViewItem();
            string[] cmd = sr.ReadLine().Split('<');
            item.Text = cmd[0];
            item.SubItems.Add(((IPEndPoint)e.RemoteEndPoint).Address.ToString());
            item.SubItems.Add(cmd[1]);
            AddServer(item);
        }
    }

    delegate void AddItem(ListViewItem item);
    private void AddServer(ListViewItem item)
    {
        if (InvokeRequired)
        {
            Invoke(new AddItem(AddServer), item);
            return;
        }
        listServer.Items.Add(item);
    }

    private void timer_tick(object sender, EventArgs e)
    {
        timer.Stop();
        foreach (var s in list)
        {
            ((Socket)s.UserToken).Dispose();
        }
    }

The simplest approach is to use Distinct() LINQ extension method: 最简单的方法是使用Distinct() LINQ扩展方法:

    IpA = IpA.Distinct().ToArray();
    for(int j = 0; j < IpA.Length; j++)
    {
    ...

You can also use a HashSet to check the duplicates. 您还可以使用HashSet来检查重复项。 I think it is a faster solution: 我认为这是一个更快的解决方案:

    HashSet<IPAddress> set = new HashSet<IPAddress>(IpA);
    for(int j = 0; j < IpA.Length; j++)
    {
        if( set.Contains(IpA[j]) )
            continue;
        set.Add(IpA[j]);

        if (IpA[j].AddressFamily == AddressFamily.InterNetwork)  //checking if IP is version 4
        {
            string scanIP = IpA[j].ToString().Substring(0, IpA[j].ToString().LastIndexOf(".")) + ".";
            Parallel.For(1, 255, (i, loopState) =>
                {
                    Scan(scanIP + i);
                });
        }
    }

You could employ HashSet to keep already used addresses. 您可以使用HashSet来保留已使用的地址。

HashSet<string> usedIP = new HashSet<string>();

Replace SomeCondition with 将SomeCondition替换为

if (!usedIp.Contains(scanIP))
{
   usedIP.Add(scanIP);
   Parallel.For(1, 255, (i, loopState) =>
                {
                     Scan(scanIP + i);
                });
}

add scanIP to a dictionary, and make sure it hasn't already been added. 将scanIP添加到字典中,并确保尚未添加它。

Dictionary<string, string> scannedIPs = new Dictionary<string, string>();

...

string scanIP = IpA[j].ToString().Substring(0, IpA[j].ToString().LastIndexOf(".")) + ".";
if (!scannedIPs.ContainsKey(scanIP))
{
   scannedIPs.Add(scanIP, scanIP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM