Beispiel #1
0
 /// <summary>Отмена сканирования</summary>
 public void stopScann()
 {
     progress_Bar.Value = 100;
     ThreadReceiver.Abort();
     sock.Close();
 }
Beispiel #2
0
        void packetReceive()
        {
            byte[] data          = new byte[65535]; // max udp size datagram
            int    recv          = 0;
            bool   foundchannel  = false;
            bool   timedout      = false;
            bool   searchforward = Ethernet.searchForward(IPstart, IPstop);
            uint   progressfull  = Ethernet.calculateNumOfIpAddr(IPstart, IPstop);
            bool   stopcondition = false;

            byte[] oct = IPstart.GetAddressBytes();
            multiep = new IPEndPoint(new IPAddress(oct), Port);
            EndPoint   ep  = (EndPoint)multiep;
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(localhost), Port);

            int  i = 0;
            int  dir, tst;
            byte str;

            if (searchforward)
            {
                dir = 1;
                tst = 256;
                str = (byte)0;
            }
            else
            {
                dir = -1;
                tst = -1;
                str = (byte)255;
            }

            while (true)
            {
                i++;
                Thread.Sleep(300); // 300 ms between requests
                foundchannel = false;
                sock         = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                try
                {
                    sock.Bind(iep);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
                    {
                        break;
                    }
                }
                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout);
                sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.Parse(localhost).GetAddressBytes());

                try
                {
                    // Must be valid multicast address, else exception 10049
                    sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(new IPAddress(oct), IPAddress.Parse(localhost)));
                }
                catch (SocketException) { break; }

                CurrentIP_label.Invoke(new UpdateTextCallback(UpdateipLabel), curip.ToString());
                FoundIP_label.Invoke(new UpdateTextCallback(UpdatefoundLabel), found.ToString());

                try
                {
                    recv = sock.ReceiveFrom(data, ref ep);
                }
                catch (SocketException ex1)
                {
                    if (ex1.SocketErrorCode == SocketError.TimedOut)
                    {
                        timedout     = true;
                        recv         = 0;
                        foundchannel = false;
                    }
                }

                if (recv > 0)
                {
                    foundchannel = true;
                    found++;
                }//We found a channel

                if (!timedout)
                {
                    Thread.Sleep(300); // we are receiving for 300 ms
                }
                string tvgName    = "Chan " + lastchan;
                string tvglogo    = "New Logo";
                string groupTitle = "New Group";
                string Name       = lastchan.ToString();
                string udp        = "udp://@" + curip.ToString() + ":" + Port;

                TVChannel tvc =
                    new TVChannel(
                        _tvgName: tvgName.Trim(),
                        _tvglogo: tvglogo.Trim(),
                        _groupTitle: groupTitle.Trim(),
                        _udp: udp.Trim(),
                        _Name: Name.Trim()
                        );

                bool isTVC = false;

                for (int count = 0; count < FindCH.Count; count++)
                {
                    if (FindCH[count].Equals(tvc))
                    {
                        isTVC = true;
                        break;
                    }
                }

                //Check if entry allready exists
                if (foundchannel && !isTVC)
                {
                    CurrentIP_label.Invoke(new RefreshDatatableViewCallback(TableAdd), tvc);
                    newchan++;
                    lastchan++;
                    CurrentIP_label.Invoke(new UpdateTextCallback(UpdateipLabel), curip.ToString());
                    FoundIP_label.Invoke(new UpdateTextCallback(UpdatefoundLabel), found.ToString());
                }

                sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(new IPAddress(oct), IPAddress.Parse(localhost)));

                if (stopcondition)
                {
                    break;
                }

                int test1 = oct[3] + dir;
                if (test1 == tst)
                {
                    oct[3] = str;
                    int test2 = oct[2] + dir;
                    if (test2 == tst)
                    {
                        oct[2] = str;
                        int test3 = oct[1] + dir;
                        if (test3 == tst)
                        {
                            oct[1] = str;
                            int test4 = oct[0] + dir;
                            if (test4 == tst)
                            {
                                oct[0] = str;
                            }
                        }
                        else
                        {
                            oct[1] = (byte)(oct[1] + dir);
                        }
                    }
                    else
                    {
                        oct[2] = (byte)(oct[2] + dir);
                    }
                }
                else
                {
                    oct[3] = (byte)(oct[3] + dir);
                }

                curip = new IPAddress(oct);
                progress_Bar.Invoke(new ProgressbarCallback(UpdateprogressBar), (int)Math.Round(100 * (float)i / (float)progressfull));

                if (curip.Equals(IPstop))
                {
                    stopcondition = true;
                }
                sock.Close();
            }

            sock.Close();

            CurrentIP_label.Invoke(new UpdateTextCallback(UpdateipLabel), curip.ToString());
            FoundIP_label.Invoke(new UpdateTextCallback(UpdatefoundLabel), found.ToString());
            start_bt.Invoke(new UpdateButtonsCallback(UpdatestartButton));
            stop_bt.Invoke(new UpdateButtonsCallback(UpdatestopButton));
            progress_Bar.Invoke(new ProgressbarCallback(UpdateprogressBar), 100);

            //Abort thread
            ThreadReceiver.Abort();
        }
Beispiel #3
0
 static void Main(string[] args)
 {
     ThreadReceiver obj = new ThreadReceiver();
     obj.CheckList();
 }