NetworkService/Program.cs

135 lines
5.1 KiB
C#
Raw Normal View History

2022-09-04 08:35:19 +09:00
using System.Net;
using System.Net.NetworkInformation;
namespace NWService
{
public class Program
{
public static void Main()
{
try
{
LanguageXml language = LanguageXmlLoader.Load();
ServiceMode mode = OptionConsole.GetServiceMode(language);
List<IPAddress> localIPv4 = NetworkService.GetLocalIPv4().ToList();
IPAddress targetIPv4 = OptionConsole.GetIPAddress(localIPv4, language);
switch(mode)
{
case ServiceMode.SEARCH_ARP:
Search_ARP(targetIPv4, language);
break;
case ServiceMode.SEARCH_PING:
Search_Ping(targetIPv4, language);
break;
default:
Console.WriteLine("unknown mode...");
break;
}
}
catch(Exception error)
{
Console.WriteLine(error.Message);
}
}
public static void Search_ARP(IPAddress targetIPv4, LanguageXml language)
{
try
{
List<DeviceInfo> findList = new List<DeviceInfo>();
Task task = Task.Run(() => {
findList.AddRange(NetworkService.SearchARP(targetIPv4));
Thread.Sleep(10);
});
string[] frames = {"","",""};
int frameCount = 0;
(int cpL, int cpT) = Console.GetCursorPosition();
while (!task.IsCompleted)
{
Console.SetCursorPosition(cpL, cpT);
frameCount++;
int targetFrameNumber = frameCount%frames.Length;
Console.WriteLine("{0} {1}", frames[targetFrameNumber], language.GetMessage("searching", "searching..."));
Thread.Sleep(100);
}
Console.SetCursorPosition(cpL, cpT);
Console.WriteLine(" ");
if (findList.Count() == 0)
{
Console.WriteLine("{0}", language.GetMessage("empty_find_list", "Not found..."));
}
else
{
Console.WriteLine(string.Format(language.GetMessage("found_devices","Found {0} Devices."), findList.Count()));
foreach (DeviceInfo deviceInfo in findList)
{
Console.WriteLine("――――――――――――――――――――――――――――");
Console.WriteLine("IP: {0}", deviceInfo.GetIPAddress().ToString());
Console.WriteLine("Mac: {0}", deviceInfo.GetMacAddress());
}
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
public static void Search_Ping(IPAddress targetIPv4, LanguageXml language)
{
try
{
List<PingReply> findList = new List<PingReply>();
Task task = Task.Run(() => {
findList.AddRange(NetworkService.SearchByPing(targetIPv4));
Thread.Sleep(10);
});
string[] frames = {"","",""};
int frameCount = 0;
(int cpL, int cpT) = Console.GetCursorPosition();
while (!task.IsCompleted)
{
Console.SetCursorPosition(cpL, cpT);
frameCount++;
int targetFrameNumber = frameCount%frames.Length;
Console.WriteLine("{0} {1}", frames[targetFrameNumber], language.GetMessage("searching", "searching..."));
Thread.Sleep(100);
}
Console.SetCursorPosition(cpL, cpT);
Console.WriteLine(" ");
if (findList.Count() == 0)
{
Console.WriteLine("{0}", language.GetMessage("empty_find_list", "Not found..."));
}
else
{
Console.WriteLine(string.Format(language.GetMessage("found_devices","Found {0} Devices."), findList.Count()));
foreach (var reply in findList)
{
Console.WriteLine("――――――――――――――――――――――――――――");
Console.WriteLine("IP: {0}", reply.Address.ToString());
Console.WriteLine("Time: {0}ms", reply.RoundtripTime);
Console.WriteLine("Status: {0}", reply.Status);
Console.WriteLine("Ttl: {0}", reply.Options.Ttl);
}
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}