Example #1
0
        private static void Send(IPAddress source, IPAddress destination, object payload)
        {
            using (UdpClient client = new UdpClient(new IPEndPoint(source, 0)))
            {
                string jsonPayload = JsonConvert.SerializeObject(payload);

                Packet packet = new Packet();
                packet.Descriptor = payload.GetType().FullName;
                packet.Payload = jsonPayload;

                string jsonPacket = JsonConvert.SerializeObject(packet);

                byte[] bytes = Encoding.ASCII.GetBytes(jsonPacket);

                IPEndPoint ip = new IPEndPoint(destination, Config.BroadcastPort);
                try
                {
                    client.Send(bytes, bytes.Length, ip);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.HostUnreachable)
                        //reasoning: we broadcast on all interfaces
                        //on OS X this may result in No route to host
                        Console.WriteLine(String.Format("{0}: {1}", source, ex.Message));
                    else
                        throw;
                }
                finally
                {
                    client.Close();
                }
            }
        }
Example #2
0
        public static void Send(IPAddress ipAddress, object payload)
        {
            using (UdpClient client = new UdpClient())
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                string jsonPayload = serializer.Serialize(payload);

                Packet packet = new Packet();
                packet.Descriptor = payload.GetType().FullName;
                packet.Payload = jsonPayload;

                string jsonPacket = serializer.Serialize(packet);

                byte[] bytes = Encoding.ASCII.GetBytes(jsonPacket);

                IPEndPoint ip = new IPEndPoint(ipAddress, Config.BroadcastPort);
                client.Send(bytes, bytes.Length, ip);
                client.Close();
            }
        }