27 lines
794 B
C#
27 lines
794 B
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Backend.Helper;
|
|
|
|
public static class TcpClientHelper
|
|
{
|
|
public static (int, int) CheckPort(string ip, params int[] ports)
|
|
{
|
|
// This would be way cleaner if the TcpClient didn't throw an exception if the destination couldn't be reached,
|
|
// and it would just return a result.error, for example.
|
|
for (int i = 0; i < ports.Length; i++) {
|
|
try
|
|
{
|
|
using TcpClient client = new();
|
|
client.Connect(ip, ports[i]);
|
|
// If the connection is successful, update the result array with the port number
|
|
}
|
|
catch
|
|
{
|
|
ports[i] = 0;
|
|
}
|
|
}
|
|
|
|
return (ports[0], ports[1]);
|
|
}
|
|
} |