Esempio n. 1
0
        public static void Main()
        {
            /* Use this for Static IP and No DHCP */
            //_eth.Name = "Quail";
            //_eth.MacAddress = _eth.GenerateUniqueMacAddress("Quail");
            //_eth.IPAddress = "192.168.1.95".ToBytes();
            //_eth.DefaultGateway = "192.168.1.1".ToBytes();
            //_eth.PreferredDomainNameServer = "8.8.8.8".ToBytes(); // Google DNS Servers
            //_eth.AlternateDomainNameServer = "8.8.8.4".ToBytes(); // Google DNS Servers
            //_eth.DHCPDisabled = true;

            _eth = new EthClick(Hardware.SocketOne);

            _eth.Start(_eth.GenerateUniqueMacAddress("MikroBusNet"), "MikroBusNet");

            while (true)
            {
                if (_eth.ConnectedToInternet)
                {
                    Debug.Print("Connected to Internet");
                    Thread.Sleep(500);
                    break;
                }
                Debug.Print("Waiting on Internet connection");
            }

            // Listen for UDP messages sent to activated ports
            EthClick.OnUdpPacketReceived += Adapter_OnUdpPacketReceived;

            // Activate the NTP (date/time) port 123
            _eth.ListenToPort(123);

            // Create a NTP (date/time) Request Message
            var msg = new byte[48];

            msg[0] = 0x1B;

            var ipAddress = "165.193.126.229".ToBytes();             // IPAddress for NIST Time Server in Weehawken, NJ USA see http://tf.nist.gov/tf-cgi/servers.cgi

            // Let's get the UTC time from a time server using a UDP Message
            UDP.SendUdpMessage(msg, ipAddress, 123, 123);

            Thread.Sleep(Timeout.Infinite);
        }
Esempio n. 2
0
        private void SendUdpMessage(Message message)
        {
            try
            {
                if (string.IsNullOrEmpty(DefaultIP))
                {
                    return;
                }

                byte[] data = message.ToPacket().ToBytes();
                //如何确定udpclient是否可用?是不是总能够保证正确的初始化?
                if (data != null)
                {
                    Udp.SendUdpMessage(data, data.Length, _defaultIP, Env.DefaultBroadCastPort);
                    //单点发送不出去是防火墙的问题,并不需要这样发多次。没什么意义。
                    //Thread.Sleep(10);
                    //udpclient.Send(data, data.Length);
                }
            }
            catch (Exception ex)
            {
                Env.Instance.Logger.Error(ex, "Send Udp message exception" + message.ToString());
            }
        }