public void StopAlarm() { try { if (m_client != null) { NetworkData data = new NetworkData(); data.ClientType = ClientTypes.Pc; data.Message = "deactivate"; data.MessageType = MessageTypes.DeactivateAlarm; var serializer = new DataContractJsonSerializer(typeof(NetworkData)); serializer.WriteObject(m_client.OutputStream.AsStreamForWrite(), data); m_client.OutputStream.AsStreamForWrite().Flush(); } } catch (Exception exception) { string str = exception.Message; } }
void StartUdpListener() { m_udpListener = new UdpClient(new IPEndPoint(IPAddress.Any, 10500)) { EnableBroadcast = true, Client = { SendTimeout = 10000 } }; Task.Run(async() => { while (true) { await m_udpListener.SendAsync(new byte[64], 10, new IPEndPoint(IPAddress.Broadcast, 10500)); m_udpListener.ReceiveAsync(); if (m_udpListener.Available > 0) { var buffer = new byte[m_udpListener.Available]; m_udpListener.Client.Receive(buffer); string str = Encoding.ASCII.GetString(buffer); if (str == string.Empty) { continue; } var stream = new MemoryStream(); stream.Write(buffer, 0, buffer.Length); var serializer = new DataContractJsonSerializer(typeof(NetworkData)); NetworkData data = null; stream.Position = 0; data = (NetworkData)serializer.ReadObject(stream); m_client = new StreamSocket(); await m_client.ConnectAsync(new HostName(data.Message), "10501"); return; } } }); }