Example #1
0
        private void Open()
        {
            System.Net.Sockets.UdpClient multicastListener = null;

            if (!m_exclusive_port)
            {
                /* We need a shared multicast "listen" port. This is the 0xBAC0 port */
                /* This will enable us to have more than 1 client, on the same machine. Perhaps it's not that important though. */
                /* We (might) only receive the multicast on this. Any unicasts to this might be eaten by another local client */
                if (m_shared_conn == null)
                {
                    m_shared_conn = new Net.Sockets.UdpClient(System.Net.Sockets.AddressFamily.InterNetworkV6);
                    m_shared_conn.ExclusiveAddressUse = false;
                    m_shared_conn.Client.SetSocketOption(Net.Sockets.SocketOptionLevel.Socket, Net.Sockets.SocketOptionName.ReuseAddress, true);
                    System.Net.EndPoint ep = new System.Net.IPEndPoint(System.Net.IPAddress.IPv6Any, m_port);
                    if (!string.IsNullOrEmpty(m_local_endpoint))
                    {
                        ep = new System.Net.IPEndPoint(Net.IPAddress.Parse(m_local_endpoint), m_port);
                    }
                    m_shared_conn.Client.Bind(ep);

                    multicastListener = m_shared_conn;
                }
                /* This is our own exclusive port. We'll recieve everything sent to this. */
                /* So this is how we'll present our selves to the world */
                if (m_exclusive_conn == null)
                {
                    System.Net.EndPoint ep = new Net.IPEndPoint(System.Net.IPAddress.IPv6Any, 0);
                    if (!string.IsNullOrEmpty(m_local_endpoint))
                    {
                        ep = new Net.IPEndPoint(Net.IPAddress.Parse(m_local_endpoint), 0);
                    }
                    m_exclusive_conn = new Net.Sockets.UdpClient((Net.IPEndPoint)ep);
                }
            }
            else
            {
                System.Net.EndPoint ep = new Net.IPEndPoint(System.Net.IPAddress.IPv6Any, m_port);
                if (!string.IsNullOrEmpty(m_local_endpoint))
                {
                    ep = new Net.IPEndPoint(Net.IPAddress.Parse(m_local_endpoint), m_port);
                }
                m_exclusive_conn = new Net.Sockets.UdpClient(System.Net.Sockets.AddressFamily.InterNetworkV6);
                m_exclusive_conn.ExclusiveAddressUse = true;
                m_exclusive_conn.Client.Bind((Net.IPEndPoint)ep);

                multicastListener = m_exclusive_conn;
            }

            multicastListener.JoinMulticastGroup(IPAddress.Parse("[FF02::BAC0]"));
            multicastListener.JoinMulticastGroup(IPAddress.Parse("[FF04::BAC0]"));
            multicastListener.JoinMulticastGroup(IPAddress.Parse("[FF05::BAC0]"));
            multicastListener.JoinMulticastGroup(IPAddress.Parse("[FF08::BAC0]"));
            multicastListener.JoinMulticastGroup(IPAddress.Parse("[FF0E::BAC0]"));

            // If this option is enabled Yabe cannot see itself !
            // multicastListener.MulticastLoopback = false;

            bvlc = new BVLCV6(this, m_VMac);
        }
Example #2
0
        /// <summary>
        /// 运行程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (button1.Content.ToString() == "停止运行")
            {
                foreach (var item in states)
                {
                    //释放内存
                    ((Timer)item.UserToKen).Dispose();
                    if (!item.CurrentProcess.HasExited)
                    {
                        item.CurrentProcess.Kill();
                    }
                }
                try
                {
                    //移除组播
                    udp.DropMulticastGroup(System.Diagnostics.UDPGroup.group.ep.Address);
                    udp.Close();

                    states.Clear();
                    task.Dispose();
                }
                catch (Exception ex)
                {
                }
                button1.Content = "开始运行";
            }
            else
            {
                //当前已经采集的索引
                if (System.IO.File.Exists(ProgressFileName))
                {
                    List <string> arr = System.IO.File.ReadLines(ProgressFileName).ToList();
                    foreach (var item in Strs)
                    {
                        //得到当前项的状态如果包含则将当前项设置为 true 表示已经处理过了
                        if (arr.Contains(item.Key))
                        {
                            item.Value.IsCollectioned = true;
                        }
                    }
                }
                //初始化采集进程
                System.Linq.Enumerable.Range(0, MaxPorcess).ToList().ForEach((p) =>
                {
                    System.Threading.Thread.Sleep(40);
                    AddStates();
                });


                //开始监听
                task = System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    udp = new System.Net.Sockets.UdpClient(System.Diagnostics.UDPGroup.group.ep.Port);
                    udp.JoinMulticastGroup(System.Diagnostics.UDPGroup.group.ep.Address);
                    udp.BeginReceive(AsyncCallback, udp);
                });
                button1.Content = "停止运行";
            }
        }
Example #3
0
 /// <summary>
 /// listen to the group
 /// </summary>
 /// <param name="owner">"this" in most case</param>
 /// <param name="dgGetMsg">handles message arriving</param>
 public void StartListen(System.Windows.Forms.Control owner, Action <string, string> dgGetMsg)
 {
     Owner       = owner;
     DgGetMsg    = dgGetMsg;
     IsListening = true;
     UdpClient   = new System.Net.Sockets.UdpClient(UDPPort);
     UdpClient.JoinMulticastGroup(GroupIP);
     thUDPListener = new System.Threading.Thread(ListenHandler);
     thUDPListener.Start();
 }
Example #4
0
        public static System.Net.Sockets.UdpClient GetUdpSender(string ipAddressString, int port)
        {
            // TODO: Check
            System.Net.IPAddress socketIPAddress = System.Net.IPAddress.Parse(ipAddressString);
            int socketPort = port;

            System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
            udpClient.JoinMulticastGroup(socketIPAddress);
            udpClient.Connect(new System.Net.IPEndPoint(socketIPAddress, socketPort));

            return(udpClient);
        }
Example #5
0
        public UdpClient(string multicastIp, int port, IJsonProvider json)
        {
            var ip = IPAddress.Parse(multicastIp);
            _endPoint = new IPEndPoint(ip, port);
            _udpClient = new System.Net.Sockets.UdpClient(port);
            _udpClient.JoinMulticastGroup(ip);

            _timer = new Timer(30 * 1000); // 30 seconds
            _timer.AutoReset = true;
            _timer.Elapsed += Broadcast;

            _json = json;
        }
Example #6
0
        public static System.Net.Sockets.UdpClient GetUdpReceiver(string ipAddressString, int port)
        {
            // TODO: Check
            System.Net.IPAddress socketIPAddress = System.Net.IPAddress.Parse(ipAddressString);
            int socketPort = port;

            System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
            udpClient.Client.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReuseAddress, true);
            udpClient.JoinMulticastGroup(socketIPAddress, System.Net.IPAddress.Any);
            udpClient.Client.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, socketPort));

            return(udpClient);
        }
Example #7
0
 public Connection Connect()
 {
     try
     {
         System.Net.Sockets.UdpClient socket       = new System.Net.Sockets.UdpClient(port);
         System.Net.IPAddress         groupAddress = System.Net.Dns.GetHostEntry(group).AddressList[0];
         socket.JoinMulticastGroup((System.Net.IPAddress)groupAddress);
         return(new MulticastConnection(socket, groupAddress));
     }
     catch (System.IO.IOException e)
     {
         throw new FastConnectionException(e);
     }
 }
 public Connection Connect()
 {
     try
     {
         System.Net.Sockets.UdpClient socket = new System.Net.Sockets.UdpClient(port);
         System.Net.IPAddress groupAddress = System.Net.Dns.GetHostEntry(group).AddressList[0];
         socket.JoinMulticastGroup((System.Net.IPAddress) groupAddress);
         return new MulticastConnection(socket, groupAddress);
     }
     catch (System.IO.IOException e)
     {
         throw new FastConnectionException(e);
     }
 }
Example #9
0
        private void StartListener(object oState)
        {
            try
            {
                oUDPClient = new System.Net.Sockets.UdpClient(Convert.ToInt32(CDLLReadSetting.ReadSetting("//configuration/appSettings/add[@key='MulticastPort']")));
                oUDPClient.Client.ReceiveBufferSize = Convert.ToInt32(CDLLReadSetting.ReadSetting("//configuration/appSettings/add[@key='UDPBufferSize']"));

                System.Net.IPEndPoint oRemoteIPEndPoint = new System.Net.IPEndPoint(
                    System.Net.IPAddress.Parse(CDLLReadSetting.ReadSetting("//configuration/appSettings/add[@key='MulticastIPAddress']")),
                    0);

                oUDPClient.DontFragment = true;

                oUDPClient.JoinMulticastGroup(System.Net.IPAddress.Parse(CDLLReadSetting.ReadSetting("//configuration/appSettings/add[@key='MulticastIPAddress']")));

                while (true)
                {
                    try
                    {
                        Byte[] arTemp = oUDPClient.Receive(ref oRemoteIPEndPoint);
                        System.IO.MemoryStream oMemoryStream = new System.IO.MemoryStream(arTemp.Length);
                        oMemoryStream.Write(arTemp, 0, arTemp.Length);

                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter oBinary = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        oMemoryStream.Seek(0, System.IO.SeekOrigin.Begin);

                        ReportLog((Hashtable)oBinary.Deserialize(oMemoryStream));

                    }
                    catch (System.Threading.ThreadAbortException ex)
                    {
                        Console.Write(ex.Message);
                        //MessageBox.Show(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        //MessageBox.Show(ex.Message);
                    }

                    System.Windows.Forms.Application.DoEvents();

                    if (oUDPClient.Available == 0)
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                }

            }

            catch (System.Threading.ThreadAbortException ex)
            {
                Console.Write(ex.Message);
                //MessageBox.Show(ex.Message);
            }

            catch (Exception ex)
            {
                Console.Write(ex.Message);
                //MessageBox.Show(ex.Message);
            }
        }