using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace GetInfoSamp
{
class Program
{
static void Main(string[] args)
{
string serverAddress = "54.37.142.74:7777"; //string name = "Zaharushka";
string[] addressParts = serverAddress.Split(':');
if (addressParts.Length != 2 || !int.TryParse(addressParts[1], out int serverPort))
{
Console.WriteLine("Неверный формат адреса"); return;
}
string serverHost = addressParts[0];
try
{
IPAddress ipAddress = ResolveHostToIp(serverHost);
IPEndPoint serverEndpoint = new IPEndPoint(ipAddress, serverPort);
using (UdpClient udpClient = new UdpClient())
{
byte[] infoRequest = CreateInfoRequestPacket(ipAddress, serverPort);
udpClient.Send(infoRequest, infoRequest.Length, serverEndpoint);
Console.WriteLine("Запрос на получение информации отправлен: {0}:{1}", serverHost, serverPort);
udpClient.Client.ReceiveTimeout = 10000;
IPEndPoint remoteEndpoint = null;
byte[] response = udpClient.Receive(ref remoteEndpoint);
if (response != null && response.Length > 0)
ProcessServerInfo(response);
else
Console.WriteLine("Ответ от сервера не получен.");
}
}
catch (Exception ex)
{
Console.WriteLine("Произошла ошибка:\n" + ex.Message);
}
Console.ReadKey();
}
static IPAddress ResolveHostToIp(string host)
{
IPAddress[] ipAddresses = Dns.GetHostAddresses(host);
if (ipAddresses.Length > 0)
return ipAddresses[0];
throw new Exception("Не удалось разрешить адрес хоста.");
}
static byte[] CreateInfoRequestPacket(IPAddress ipAddress, int port)
{
byte[] request = new byte[11];
Buffer.BlockCopy(Encoding.ASCII.GetBytes("SAMP"), 0, request, 0, 4);
request[10] = (byte)'i';
return request;
}
static void ProcessServerInfo(byte[] response)
{
int byteCount = 11;
byte bytePassword = response[byteCount];
byteCount++;
ushort wOnlinePlayers = BitConverter.ToUInt16(response, byteCount);
byteCount += 2;
ushort wMaxPlayers = BitConverter.ToUInt16(response, byteCount);
byteCount += 2;
int iHostNameLen = BitConverter.ToInt32(response, byteCount);
byteCount += 4;
string szHostName = Encoding.ASCII.GetString(response, byteCount, iHostNameLen);
byteCount += iHostNameLen;
int iGameModeLen = BitConverter.ToInt32(response, byteCount);
byteCount += 4;
string szGameMode = Encoding.ASCII.GetString(response, byteCount, iGameModeLen);
byteCount += iGameModeLen;
int iMapNameLen = BitConverter.ToInt32(response, byteCount);
byteCount += 4;
string szMapName = Encoding.ASCII.GetString(response, byteCount, iMapNameLen);
Console.WriteLine("Название сервера: {0}", szHostName);
Console.WriteLine("Название мода: {0}", szGameMode);
Console.WriteLine("Карта/Язык: {0}", szMapName);
Console.WriteLine("Игроки: {0}/{1}", wOnlinePlayers, wMaxPlayers);
Console.WriteLine("Пароль: {0}", bytePassword == 1 ? "есть" : "нет");
}
}
}