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); }
public void waitForConnection() { UdpClient udpServer = new UdpClient(11000); string serverip = GetLocalIPAddress(); int port = 11000; IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(serverip), 11000); Console.WriteLine("Listening on IP " + serverip + " 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); 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]; if (value.Equals("")) { value = "Empty"; } 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("192.168.62.154", 11000); 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); } }