private void getStatus() { byte[] data = new byte[1024]; //string input, stringData; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.0.151"), 234); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Random Statreq = new Random(); int index = Statreq.Next(0, 5); Statusobject stobj = new Statusobject { Request = "GetControlStatus", RequestID = index.ToString(), Controls = new Control[] { new Control() { ID = "37", StatusValue = "255" } }, Version = "1501603666", RequestedBy = "DefaultUser" }; string output = JsonConvert.SerializeObject(stobj); data = Encoding.ASCII.GetBytes(output); server.SendTo(data, data.Length, SocketFlags.None, ipep); //Send Request to Server EndPoint Remote = (EndPoint) new IPEndPoint(IPAddress.Any, 0); data = new byte[1024]; int recv = server.ReceiveFrom(data, ref Remote); //Reseive Response from Server Response.InnerHtml = Server.HtmlEncode("Message received from {0}:" + Remote); string json = data.ToString(); ResponseObj Deserial = JsonConvert.DeserializeObject <ResponseObj>(Encoding.ASCII.GetString(data, 0, recv)); LinkCheck.InnerHtml = Server.HtmlEncode("\n This is output\t" + Deserial.Data.ControlStatus[0].StatusValue + "\n\nTIME" + DateTime.Now); var ha = Deserial.Data.ControlStatus[0].StatusValue; int StatuValueInt = Convert.ToInt32(ha); if (StatuValueInt > 0) { MasterBedroomStatus = 1; MasterBedRoomButton.BackColor = Color.White; } else { MasterBedroomStatus = 0; MasterBedRoomButton.BackColor = Color.LightGray; } }
protected void onclick(object sender, EventArgs e) { byte[] data = new byte[1024]; //string input, stringData; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.0.151"), 234); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Random Statreq1 = new Random(); int index1 = Statreq1.Next(0, 5); Requestobject obj1 = new Requestobject { Request = "LinkCheck", RequestID = index1.ToString(), Version = "1501603666" }; string output = JsonConvert.SerializeObject(obj1); Console.WriteLine(output); data = Encoding.ASCII.GetBytes(output); server.SendTo(data, data.Length, SocketFlags.None, ipep); EndPoint Remote = (EndPoint) new IPEndPoint(IPAddress.Any, 0); data = new byte[1024]; int recv = server.ReceiveFrom(data, ref Remote); Response.InnerHtml = Server.HtmlEncode("Message received from {0}:" + Remote); LinkCheck.InnerHtml = Server.HtmlEncode(Encoding.ASCII.GetString(data, 0, recv) + "TIME" + DateTime.Now); Random Statreq = new Random(); int index = Statreq.Next(0, 5); Statusobject stobj = new Statusobject { Request = "GetControlStatus", RequestID = index.ToString(), Controls = new Control[] { new Control() { ID = "37", StatusValue = "255" } }, Version = "1501603666", RequestedBy = "DefaultUser" }; output = JsonConvert.SerializeObject(stobj); Console.WriteLine(output); data = Encoding.ASCII.GetBytes(output); server.SendTo(data, data.Length, SocketFlags.None, ipep); sender = new IPEndPoint(IPAddress.Any, 0); Remote = (EndPoint)(sender); data = new byte[1024]; recv = server.ReceiveFrom(data, ref Remote); Console.WriteLine("Message received from {0}:", Remote.ToString()); Console.WriteLine(recv); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); LightStatus.InnerHtml = Server.HtmlEncode(Encoding.ASCII.GetString(data, 0, recv)); }