Пример #1
0
        public void SendMessage(string json)
        {
            MulticastMessageEventArgs ev = new MulticastMessageEventArgs();

            ev.JsonString        = json;
            ev.IncomingInterface = this.incomingInterface;
            this.HandleMessage(this, ev);
        }
Пример #2
0
        public MulticastMessageReceiver(IPAddress address, int port)
        {
            this.multicastIP         = address;
            this.interfaceMap        = new Dictionary <int, NetworkInterface>();
            this.multicastInterfaces = new List <IPAddress>();

            this.socket    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this.eventArgs = new MulticastMessageEventArgs();
            try
            {
                this.socket.ReceiveBufferSize = 128000;
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
                this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
                this.socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, 0);
                this.socket.Bind(ipep);

                this.receiveBuffer = new byte[65536];
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                this.endPoint = (EndPoint)sender;

                NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(this.AddressChangedCallback);

                this.AddMulticastMembership();

                this.socket.BeginReceiveMessageFrom(
                    this.receiveBuffer,
                    0,
                    this.receiveBuffer.Length,
                    SocketFlags.None,
                    ref this.endPoint,
                    new AsyncCallback(this.MessageComplete),
                    null);
            }
            catch
            {
                this.socket.Dispose();
                throw;
            }
        }