コード例 #1
0
        public override void Send(byte[] data, string to = "")
        {
            try
            {
                var endpoint = new IPEndPoint(_address, _port);

                ClientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, endpoint, OnSendToCallback,
                                         ClientSocket);
                OnReportingStatus(StatusCode.Info, "Started sending UDP broadcast message");
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException socketException)
            {
                OnCaughtException(socketException, EventCode.Send);
            }
            catch (Exception exception)
            {
                OnCaughtException(exception, EventCode.Other);
            }
        }
コード例 #2
0
        public override void Send(byte[] data, string to = "")
        {
            try
            {
                var endpoint = new IPEndPoint(_multicastAddress, _multicastPort);
                if (!string.IsNullOrEmpty(to))
                {
                    var ep = IPEndPoint.Parse(to);
                    if ((!ep.Address.Equals(_multicastAddress) || ep.Port != _multicastPort) &&
                        ep.Address.ToString().IsMulticastAddress())
                    {
                        var old = endpoint;
                        endpoint          = ep;
                        _multicastAddress = endpoint.Address;
                        _multicastPort    = endpoint.Port;

                        OnReportingStatus(StatusCode.Info,
                                          $"Changed multicast address and port from {old} to {endpoint}");
                    }
                }

                ClientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, endpoint, OnSendToCallback,
                                         ClientSocket);
                OnReportingStatus(StatusCode.Info,
                                  $"Started sending {data.Length} bytes via UDP socket in multicast mode");
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException socketException)
            {
                OnCaughtException(socketException, EventCode.Send);
            }
            catch (Exception exception)
            {
                OnCaughtException(exception, EventCode.Other);
            }
        }
コード例 #3
0
        public override void Send(byte[] data, string to = "")
        {
            if (!string.IsNullOrEmpty(to))
            {
                var strings = to.Split(':');
                try
                {
                    var ipAddress = IPAddress.Parse(strings[0]);
                    var port      = int.Parse(strings[1]);
                    _endPoint  = new IPEndPoint(ipAddress, port);
                    _listening = false;
                }
                catch (Exception e)
                {
                    OnCaughtException(e, EventCode.Other);
                    return;
                }
            }

            try
            {
                ClientSocket.BeginSendTo(data, 0, data.Length, 0, _endPoint, OnSendToCallback, ClientSocket);
                OnReportingStatus(StatusCode.Info,
                                  $"Started sending {data.Length} bytes to {_endPoint} via UDP socket");
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException socketException)
            {
                OnCaughtException(socketException, EventCode.Send);
            }
            catch (Exception e)
            {
                OnCaughtException(e, EventCode.Other);
            }
        }