Exemple #1
0
        public getObject ReadToObject(byte[] json)
        {
            getObject    deserializedUser = new getObject();
            MemoryStream ms = new MemoryStream(json);
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());

            deserializedUser = ser.ReadObject(ms) as getObject;
            ms.Close();
            return(deserializedUser);
        }
Exemple #2
0
        public void waitForConnection()
        {
            UdpClient  udpServer = new UdpClient(11000);
            string     serverip  = GetLocalIPAddress();
            int        port      = 11000;
            IPEndPoint endPoint  = new IPEndPoint(IPAddress.Parse("192.168.62.154"), 11000);

            Console.WriteLine("Listening on IP " + "192.168.62.154" + " port: " + port);

            while (true)
            {
                var    receive = udpServer.Receive(ref endPoint);
                string time    = null;
                time = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(time + " Received value is: " + Encoding.ASCII.GetString(receive));
                string    sentence   = null;
                getObject get        = ReadToObject(receive);
                string    ip_Address = get.Ip;

                if (get.DeviceName == "Daniel")
                {
                    if (get.ElementName == "sysDescr")
                    {
                        sentence = ".1.3.6.1.2.1.1.1.0";
                    }
                    else if (get.ElementName == "sysObjectID")
                    {
                        sentence = ".1.3.6.1.2.1.1.2.0";
                    }
                    else if (get.ElementName == "sysUpTime")
                    {
                        sentence = ".1.3.6.1.2.1.1.3.0";
                    }
                    else if (get.ElementName == "icmpInMsgs")
                    {
                        sentence = ".1.3.6.1.2.1.5.1.0";
                    }
                    else if (get.ElementName == "sysName")
                    {
                        sentence = ".1.3.6.1.2.1.1.5.0";
                    }
                    else if (get.ElementName == "icmpInDestUnreachs")
                    {
                        sentence = ".1.3.6.1.2.1.5.3.0";
                    }
                    else if (get.ElementName == "sysServices")
                    {
                        sentence = ".1.3.6.1.2.1.1.7.0";
                    }
                    string value = snmp.get(sentence)[2];
                    string oid   = snmp.get(sentence)[0];
                    string type  = snmp.get(sentence)[1];

                    time = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
                    Console.ForegroundColor = ConsoleColor.Red;



                    //string receivedMessage = Encoding.ASCII.GetString(receive);
                    //string ipAddress = receivedMessage.Split('#')[1];

                    var client = new UdpClient(get.Ip, 11001);

                    byte[] msg = WriteFromObject(new SnmpTypeObject(oid, value, type));
                    Console.WriteLine(time + " Sending JSON object :" + Encoding.ASCII.GetString(msg));
                    int size = msg.Length;
                    client.Send(msg, size);
                }
                else
                {
                    var clientProxy = new UdpClient("192.168.60.84", 11000);
                    clientProxy.Send(receive, receive.Length);


                    IPEndPoint endPoint2 = new IPEndPoint(IPAddress.Parse(serverip), 11000);
                    receive = udpServer.Receive(ref endPoint);
                    time    = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(time + " Sending JSON object :" + Encoding.ASCII.GetString(receive));
                    var client2 = new UdpClient(ip_Address, 11001);
                    client2.Send(receive, receive.Length);
                }
            }
        }