public static void SendPacket(ISCPPacket packet, bool blocking = false) {
   if (blocking) {
     blocked = true;
     OnPacketRecieved -= blockingListen;
     OnPacketRecieved += blockingListen;
   }
   checkConnect();
   if (_sock != null && _sock.Connected) {
     _sock.Send(packet.GetBytes(), 0, packet.GetBytes().Length, SocketFlags.None);
   }
   while (blocked) Thread.Sleep(100);
   Thread.Sleep(100);
 }
    public static DiscoveryResult DiscoverDevice(int port) {
      IEnumerable<string> ips = getInterfaceAddresses();
      var ret = new DiscoveryResult();
      _localEndPoint = new IPEndPoint(IPAddress.Any, 0);
      _client = new UdpClient(port);

      var p = new ISCPPacket("!xECNQSTN");
      byte[] sendbuf = p.GetBytes();
      foreach (string networkaddress in ips) {
        _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
        _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
        _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
      }
      while (_client.Available > 0) {
        byte[] recv = _client.Receive(ref _localEndPoint);
        Thread.Sleep(100);
        var sb = new StringBuilder();
        foreach (byte t in recv)
          sb.Append(char.ConvertFromUtf32(Convert.ToInt32(string.Format("{0:x2}", t), 16)));
        string stringData = sb.ToString();
        if (stringData.Contains("!1ECN")) {
          int idx = stringData.IndexOf("!1ECN") + 5;
          string[] parts = stringData.Substring(idx).Split('/');
          string mac = parts[3].Substring(0,12);
          string ip = ARP.GetIPInfo(mac).IPAddress;
          ret.IP = ip;
          ret.Port = Convert.ToInt32(parts[1]);
          ret.Region = stringData.Substring(idx + 14, 2);
          ret.MAC = mac;
          ret.Model = stringData.Substring(idx, 7);
        }
      }
      _client.Close();
      return ret;
    }
예제 #3
0
 public static void SendPacket(ISCPPacket packet, bool blocking = false)
 {
     if (blocking)
     {
         blocked           = true;
         OnPacketRecieved -= blockingListen;
         OnPacketRecieved += blockingListen;
     }
     checkConnect();
     if (_sock != null && _sock.Connected)
     {
         _sock.Send(packet.GetBytes(), 0, packet.GetBytes().Length, SocketFlags.None);
     }
     while (blocked)
     {
         Thread.Sleep(100);
     }
     Thread.Sleep(100);
 }
        public static DiscoveryResult DiscoverDevice(int port)
        {
            IEnumerable <string> ips = getInterfaceAddresses();
            var ret = new DiscoveryResult();

            _localEndPoint = new IPEndPoint(IPAddress.Any, 0);
            _client        = new UdpClient(port);

            var p = new ISCPPacket("!xECNQSTN");

            byte[] sendbuf = p.GetBytes();
            foreach (string networkaddress in ips)
            {
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
                _client.Send(sendbuf, sendbuf.Length, IPAddress.Parse(networkaddress).ToString(), port);
            }
            while (_client.Available > 0)
            {
                byte[] recv = _client.Receive(ref _localEndPoint);
                Thread.Sleep(100);
                var sb = new StringBuilder();
                foreach (byte t in recv)
                {
                    sb.Append(char.ConvertFromUtf32(Convert.ToInt32(string.Format("{0:x2}", t), 16)));
                }
                string stringData = sb.ToString();
                if (stringData.Contains("!1ECN"))
                {
                    int      idx   = stringData.IndexOf("!1ECN") + 5;
                    string[] parts = stringData.Substring(idx).Split('/');
                    string   mac   = parts[3].Substring(0, 12);
                    string   ip    = ARP.GetIPInfo(mac).IPAddress;
                    ret.IP     = ip;
                    ret.Port   = Convert.ToInt32(parts[1]);
                    ret.Region = stringData.Substring(idx + 14, 2);
                    ret.MAC    = mac;
                    ret.Model  = stringData.Substring(idx, 7);
                }
            }
            _client.Close();
            return(ret);
        }