All pastes #953911 Raw Edit

Someone

public text v1 · immutable
#953911 ·published 2008-03-23 14:28 UTC
rendered paste body
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace SourceQueryConsole
{
    class Program
    {
        static bool Continue = true;
        static string Ret = "";

        static void QueryServer( object data )
        {
            Hashtable h = ( Hashtable )data;
            string Server; int Port;
            Server = ( string )h[ "host" ];
            Port = ( int )h[ "port" ];

            UdpClient c = new UdpClient( );
            try
            {
                c.Connect( Server, Port );
                byte[ ] bytes = { 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 };
                c.Send( bytes, bytes.Length );

                IPEndPoint ip = new IPEndPoint( IPAddress.Any, 0 );
                byte[ ] recv = c.Receive( ref ip );

                string recvStr = Encoding.ASCII.GetString( recv ).Remove( 0, 6 );
                string[ ] tmp = recvStr.Split( '\0' );
                int len = 0;
                for ( int i = 0; i < 6; i++ )
                {
                    len += tmp[ i ].Length;
                }
                len += 6;

                string hostname = tmp[ 0 ];
                string map = tmp[ 1 ];
                string game = tmp[ 3 ];

                string players = recv[ len ].ToString( );
                string maxplayers = recv[ len + 1 ].ToString( );

                c.Close( );
                Ret = "Name: " + hostname + ".\nMap: " + map + ".\nGame: " + game + ".\nPlayers/Maxplayers: " + players + "/" + maxplayers + ".";
                Continue = false;
            }
            catch ( Exception ex )
            {
                Console.WriteLine( "Unable to connect." );
                Continue = false;
            }
        }

        static void Main( string[ ] args )
        {
            Console.Write( "Enter the server followed by the port(SERVER:PORT): " );
            string Read = Console.ReadLine( );
            int colon = Read.IndexOf( ':' );
            int port = 27015;
            if ( colon != -1 )
                port = Convert.ToInt32( Read.Substring( colon + 1 ) );

            Hashtable pass = new Hashtable( );
            pass[ "host" ] = Read.Substring( 0, colon );
            pass[ "port" ] = port;

            Thread th = new Thread( QueryServer );
            th.Start( pass );
            while ( Continue ) { }
            th.Abort( );

            if ( Ret != "" )
                Console.WriteLine( Ret );
            else
                Console.WriteLine( "Something went wrong." );

            Console.Read( );
        }
    }
}