# [Snippet][C#.NET] Steam A2S_INFO query



## FordGT90Concept (Jan 3, 2017)

I can't believe something that does this isn't readily available on the internet so, I wrote it and here you go:

```
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace ServerQuery
{
    /// <summary>Adopted from Valve description: https://developer.valvesoftware.com/wiki/Server_queries#A2S_INFO </summary>
    public class A2S_INFO
    {
        // \xFF\xFF\xFF\xFFTSource Engine Query\x00 because UTF-8 doesn't like to encode 0xFF
        public static readonly byte[] REQUEST = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00 };
        #region Strong Typing Enumerators
        [Flags]
        public enum ExtraDataFlags : byte
        {
            GameID = 0x01,
            SteamID = 0x10,
            Keywords = 0x20,
            Spectator = 0x40,
            Port = 0x80
        }
        public enum VACFlags : byte
        {
            Unsecured = 0,
            Secured = 1
        }
        public enum VisibilityFlags : byte
        {
            Public = 0,
            Private = 1
        }
        public enum EnvironmentFlags : byte
        {
            Linux = 0x6C,   //l
            Windows = 0x77, //w
            Mac = 0x6D,     //m
            MacOsX = 0x6F   //o
        }
        public enum ServerTypeFlags : byte
        {
            Dedicated = 0x64,     //d
            Nondedicated = 0x6C,   //l
            SourceTV = 0x70   //p
        }
        #endregion
        public byte Header { get; set; }        // I
        public byte Protocol { get; set; }
        public string Name { get; set; }
        public string Map { get; set; }
        public string Folder { get; set; }
        public string Game { get; set; }
        public short ID { get; set; }
        public byte Players { get; set; }
        public byte MaxPlayers { get; set; }
        public byte Bots { get; set; }
        public ServerTypeFlags ServerType { get; set; }
        public EnvironmentFlags Environment { get; set; }
        public VisibilityFlags Visibility { get; set; }
        public VACFlags VAC { get; set; }
        public string Version { get; set; }
        public ExtraDataFlags ExtraDataFlag { get; set; }
        #region Extra Data Flag Members
        public ulong GameID { get; set; }           //0x01
        public ulong SteamID { get; set; }          //0x10
        public string Keywords { get; set; }        //0x20
        public string Spectator { get; set; }       //0x40
        public short SpectatorPort { get; set; }   //0x40
        public short Port { get; set; }             //0x80
        #endregion
        public A2S_INFO(IPEndPoint ep)
        {
            UdpClient udp = new UdpClient();
            udp.Send(REQUEST, REQUEST.Length, ep);
            MemoryStream ms = new MemoryStream(udp.Receive(ref ep));    // Saves the received data in a memory buffer
            BinaryReader br = new BinaryReader(ms, Encoding.UTF8);      // A binary reader that treats charaters as Unicode 8-bit
            ms.Seek(4, SeekOrigin.Begin);   // skip the 4 0xFFs
            Header = br.ReadByte();
            Protocol = br.ReadByte();
            Name = ReadNullTerminatedString(ref br);
            Map = ReadNullTerminatedString(ref br);
            Folder = ReadNullTerminatedString(ref br);
            Game = ReadNullTerminatedString(ref br);
            ID = br.ReadInt16();
            Players = br.ReadByte();
            MaxPlayers = br.ReadByte();
            Bots = br.ReadByte();
            ServerType = (ServerTypeFlags)br.ReadByte();
            Environment = (EnvironmentFlags)br.ReadByte();
            Visibility = (VisibilityFlags)br.ReadByte();
            VAC = (VACFlags)br.ReadByte();
            Version = ReadNullTerminatedString(ref br);
            ExtraDataFlag = (ExtraDataFlags)br.ReadByte();
            #region These EDF readers have to be in this order because that's the way they are reported
            if (ExtraDataFlag.HasFlag(ExtraDataFlags.Port))
                Port = br.ReadInt16();
            if (ExtraDataFlag.HasFlag(ExtraDataFlags.SteamID))
                SteamID = br.ReadUInt64();
            if (ExtraDataFlag.HasFlag(ExtraDataFlags.Spectator))
            {
                SpectatorPort = br.ReadInt16();
                Spectator = ReadNullTerminatedString(ref br);
            }
            if (ExtraDataFlag.HasFlag(ExtraDataFlags.Keywords))
                Keywords = ReadNullTerminatedString(ref br);
            if (ExtraDataFlag.HasFlag(ExtraDataFlags.GameID))
                GameID = br.ReadUInt64();
            #endregion
            br.Close();
            ms.Close();
            udp.Close();
        }
        /// <summary>Reads a null-terminated string into a .NET Framework compatible string.</summary>
        /// <param name="input">Binary reader to pull the null-terminated string from.  Make sure it is correctly positioned in the stream before calling.</param>
        /// <returns>String of the same encoding as the input BinaryReader.</returns>
        public static string ReadNullTerminatedString(ref BinaryReader input)
        {
            StringBuilder sb = new StringBuilder();
            char read = input.ReadChar();
            while (read != '\x00')
            {
                sb.Append(read);
                read = input.ReadChar();
            }
            return sb.ToString();
        }
    }
}
```
To use it, just call new A2S_INFO(IPEndPoint) and, if it doesn't throw an exception, it should have all the data in the class instance.  If it throws an exception, you're either querying the wrong port number (usually not the game port number) or the server is unavailable.


----------



## FordGT90Concept (Nov 27, 2019)

By request, here's A2S_PLAYER:

```
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace ServerQuery
{
    /// <summary>Adopted from Valve description: https://developer.valvesoftware.com/wiki/Server_queries#A2S_PLAYER </summary>
    public class A2S_PLAYER
    {
        public struct Player
        {
            public Player(ref BinaryReader br)
            {
                Index = br.ReadByte();
                Name = ReadNullTerminatedString(ref br);
                Score = br.ReadInt32();
                Duration = br.ReadSingle();
            }
            public byte Index { get; set; }
            public string Name { get; set; }
            public int Score { get; set; }
            public float Duration { get; set; }

            public override string ToString()
            {
                return Name;
            }
        }

        // \xFF\xFF\xFF\xFFU\xFF\xFF\xFF\xFF
        public static readonly byte[] REQUEST = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x55, 0xFF, 0xFF, 0xFF, 0xFF };

        public byte Header { get; set; }        // D
        public Player[] Players { get; set; }

        public A2S_PLAYER(IPEndPoint ep)
        {
            UdpClient udp = new UdpClient();
            udp.Send(REQUEST, REQUEST.Length, ep); // Request Challenge.
            byte[] challenge_response = udp.Receive(ref ep);
            if (challenge_response.Length == 9 && challenge_response[4] == 0x41) //B
            {
                challenge_response[4] = 0x55; //U
                // \xFF\xFF\xFF\xFFU[CHALLENGE]
                udp.Send(challenge_response, challenge_response.Length, ep); // Request data.

                MemoryStream ms = new MemoryStream(udp.Receive(ref ep));    // Saves the received data in a memory buffer
                BinaryReader br = new BinaryReader(ms, Encoding.UTF8);      // A binary reader that treats charaters as Unicode 8-bit
                ms.Seek(4, SeekOrigin.Begin);   // skip the 4 0xFFs
                Header = br.ReadByte(); // D
                Players = new Player[br.ReadByte()];
                for (int i = 0; i < Players.Length; i++)
                    Players[i] = new Player(ref br);
                br.Close();
                ms.Close();
                udp.Close();
            }
            else
                throw new Exception("Response invalid.");

        }

        /// <summary>Reads a null-terminated string into a .NET Framework compatible string.</summary>
        /// <param name="input">Binary reader to pull the null-terminated string from.  Make sure it is correctly positioned in the stream before calling.</param>
        /// <returns>String of the same encoding as the input BinaryReader.</returns>
        public static string ReadNullTerminatedString(ref BinaryReader input)
        {
            StringBuilder sb = new StringBuilder();
            char read = input.ReadChar();
            while (read != '\x00')
            {
                sb.Append(read);
                read = input.ReadChar();
            }
            return sb.ToString();
        }
    }
}
```


----------



## FordGT90Concept (Jul 9, 2020)

Was asked how to query master server list so...

This requires .NET Core 3.x or newer in order to get access to System.Text.Json.  Alternatively, you'll have to use NuGet to get that package.  The two code snippets above work fine in .NET Core 3.x.

```
using System.Text.Json;
using System.Net;

namespace Steam
{
    public class IGameServersService
    {
        public static server[] GetServerList(string url)
        {
            WebClient wc = new WebClient();
            string data = wc.DownloadString(url);
            query servers = JsonSerializer.Deserialize<query>(data);
            return servers.response.servers;
        }
    }
    public class query
    {
        public response response { get; set; }
    }
    public class response
    {
        public server[] servers { get; set; }
    }
    public class server
    {
        public string addr { get; set; }
        public ushort port { get; set; }
        public string steamid { get; set; }
        public string name { get; set; }
        public int appid { get; set; }
        public string gamedir { get; set; }
        public string version { get; set; }
        public string product { get; set; }
        public int region { get; set; }
        public int players { get; set; }
        public int max_players { get; set; }
        public int bots { get; set; }
        public string map { get; set; }
        public bool secure { get; set; }
        public bool dedicated { get; set; }
        public string os { get; set; }
        public string gametype { get; set; }

        public override string ToString()
        {
            return name + " (" + addr + ")";
        }
    }
}
```
You may have to add more elements to the server class above.  It's definitely not an exhaustive list.

To invoke, call this where %APIKEY% is replaced with your Steam Web API key and %APPID% is replaced with the Steam application ID of the game you're intending to query:

```
Steam.IGameServersService.GetServerList(@"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key=%APIKEY%&filter=appid\%APPID%&limit=10");
```
Of course can tweak that URL to match your needs based on the API's functionality.  This is just provided as an example to toss into the code above.


----------

