Beispiel #1
0
            private void NetworkWatcher(object state)
            {
                try
                {
                    List <IPAddress> currentBroadcastIPs = NetUtilities.GetBroadcastIPList();
                    List <IPAddress> newIPs = new List <IPAddress>();

                    lock (_broadcastIPs)
                    {
                        foreach (IPAddress currentIP in currentBroadcastIPs)
                        {
                            if (!_broadcastIPs.Contains(currentIP))
                            {
                                newIPs.Add(currentIP);
                            }
                        }

                        _broadcastIPs.Clear();
                        _broadcastIPs.AddRange(currentBroadcastIPs);
                    }

                    BroadcastNetworkDiscovered(newIPs.ToArray());
                }
                catch
                { }
                finally
                {
                    if (_networkWatcher != null)
                    {
                        _networkWatcher.Change(NETWORK_WATCHER_INTERVAL, Timeout.Infinite);
                    }
                }
            }
Beispiel #2
0
            public Listener(int listenerPort)
            {
                _listenerPort = listenerPort;

                _udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                _udpListener.Bind(new IPEndPoint(0, listenerPort));

                _listeningThread = new Thread(RecvDataAsync);
                _listeningThread.IsBackground = true;
                _listeningThread.Start();

                _broadcastIPs   = NetUtilities.GetBroadcastIPList();
                _networkWatcher = new Timer(NetworkWatcher, null, NETWORK_WATCHER_INTERVAL, Timeout.Infinite);
            }