Esempio n. 1
0
        public void Send(Message msg)
        {
            MemoryStream s = new MemoryStream();
            BinaryFormatter f = new BinaryFormatter();
            f.Serialize(s, msg);
            byte[] b = s.ToArray();

            IPEndPoint ipep = new IPEndPoint(multicastAddress, listeningPort);

            // Some weird memory exception occurs if I reuse the socket
            Socket sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sendingSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress));
            sendingSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, TTL);
            sendingSocket.Connect(ipep);
            sendingSocket.Send(b, b.Length, SocketFlags.None);

            if (OnMessageSent != null)
            {
                OnMessageSent(this, new MessageEventArgs(msg));
            }

            sendingSocket.Close();
        }
Esempio n. 2
0
 public MessageEventArgs(Message m)
 {
     Message = m;
 }