You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/labs/OnvifTest/Program.cs

72 lines
3.1 KiB

using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnvifTest
{
internal class Program
{
private static readonly object thisLock = new object();
[System.Diagnostics.CodeAnalysis.SuppressMessage("样式", "IDE0060:删除未使用的参数", Justification = "<挂起>")]
private static void Main(string[] args)
{
using (var tokenSource = new CancellationTokenSource())
{
var ips = NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(nic => nic.GetIPProperties().UnicastAddresses)
.SelectMany(o => o.ToList())
.Select(o => o.Address)
.Where(o => o.AddressFamily == AddressFamily.InterNetwork)
.ToList();
var ipAddress = ips.First();
using (var client = new UdpClient(new IPEndPoint(ipAddress, 0)))
{
var probeMessage = string.Format(CultureInfo.CurrentCulture, Template.GetServiceRequestTemplage, Guid.NewGuid());
var message = Encoding.UTF8.GetBytes(probeMessage);
Task.Run(() =>
{
try
{
var ep = new IPEndPoint(IPAddress.Any, 3702);
while (!tokenSource.IsCancellationRequested)
{
if (client.Available > 0)
{
var buffer = client.Receive(ref ep);
var result = Encoding.UTF8.GetString(buffer);
Console.WriteLine(result);
}
Thread.Sleep(1);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}, tokenSource.Token);
var timer = new System.Timers.Timer(5000);
timer.Elapsed += (s, e) =>
{
Console.WriteLine("send");
//client.Send(message, message.Length, new IPEndPoint(IPAddress.Parse("239.255.255.250"), 3702));
var message2 = Encoding.ASCII.GetBytes("GETIP\r\n");
client.Send(message2, message2.Length, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 9090));
};
timer.Start();
Thread.Sleep(60 * 1000);
timer.Stop();
timer.Dispose();
}
}
}
}
}