C
C#16mo ago
Jarski

❔ port scanner for game servers?

Hello, currently working on a program and one of its core functionalities is to scan the local network for game servers on a specific port (Rust, 28015), however after I have attempted to achieve this, it appears to not work. It seems to be stuck "searching" to no avail and doesnt show any results even though im hosting a server on another local machine with the required port. perhaps ive made an error in my method? Any help appreciated.
4 Replies
Jarski
JarskiOP16mo ago
private async Task PerformNetworkScanAsync()
{
await Task.Run(() =>
{
string localIpAddress = GetLocalIpAddress();
if (!string.IsNullOrEmpty(localIpAddress))
{
ScanNetworkForRustServers(localIpAddress);
}
});
}

private string GetLocalIpAddress()
{
string hostName = Dns.GetHostName();
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
foreach (IPAddress ipAddress in hostEntry.AddressList)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
{
return ipAddress.ToString();
}
}
return null;
}

private void ScanNetworkForRustServers(string localIpAddress)
{
string[] ipSegments = localIpAddress.Split('.');
if (ipSegments.Length != 4)
{
resultsListBox.Items.Add("Invalid local IP format.");
return;
}

string baseIpAddress = $"{ipSegments[0]}.{ipSegments[1]}.{ipSegments[2]}";

for (int i = 1; i <= 255; i++)
{
string ipAddress = $"{baseIpAddress}.{i}";
if (CheckPort(ipAddress, 28015))
{
resultsListBox.Items.Add($"Server found: {ipAddress}:28015");
}
}
}

private bool CheckPort(string ipAddress, int port)
{
using (TcpClient tcpClient = new TcpClient())
{
try
{
tcpClient.Connect(ipAddress, port);
return true; // Port is open
}
catch (SocketException)
{
return false; // Port is closed
}
}
}
private async Task PerformNetworkScanAsync()
{
await Task.Run(() =>
{
string localIpAddress = GetLocalIpAddress();
if (!string.IsNullOrEmpty(localIpAddress))
{
ScanNetworkForRustServers(localIpAddress);
}
});
}

private string GetLocalIpAddress()
{
string hostName = Dns.GetHostName();
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
foreach (IPAddress ipAddress in hostEntry.AddressList)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
{
return ipAddress.ToString();
}
}
return null;
}

private void ScanNetworkForRustServers(string localIpAddress)
{
string[] ipSegments = localIpAddress.Split('.');
if (ipSegments.Length != 4)
{
resultsListBox.Items.Add("Invalid local IP format.");
return;
}

string baseIpAddress = $"{ipSegments[0]}.{ipSegments[1]}.{ipSegments[2]}";

for (int i = 1; i <= 255; i++)
{
string ipAddress = $"{baseIpAddress}.{i}";
if (CheckPort(ipAddress, 28015))
{
resultsListBox.Items.Add($"Server found: {ipAddress}:28015");
}
}
}

private bool CheckPort(string ipAddress, int port)
{
using (TcpClient tcpClient = new TcpClient())
{
try
{
tcpClient.Connect(ipAddress, port);
return true; // Port is open
}
catch (SocketException)
{
return false; // Port is closed
}
}
}
cap5lut
cap5lut16mo ago
seems like u r using the wrong protocol, when i look at the port config description from https://developer.valvesoftware.com/wiki/Rust_Dedicated_Server
+server.port 28015 Sets the port the server will use. (default 28015 UDP)
Jarski
JarskiOP16mo ago
Ah. I need udp. Not tcp. Will take a look
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server