Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7.  
  8. namespace MasterServerQuery
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             try
  15.             {
  16.                 var EP = new IPEndPoint(IPAddress.Any, 0);
  17.                 var getservers = Encoding.UTF8.GetBytes("    getservers IW4 142 all");
  18.                 for (int i = 0; i < 4; i++)
  19.                     getservers[i] = 0xFF;
  20.  
  21.                 UdpClient client = new UdpClient("server.alteriw.net", 20810);
  22.                 while (true)
  23.                 {
  24.                     client.Send(getservers, getservers.Length);
  25.                     var receivedata = client.Receive(ref EP);
  26.                     if (EP.Address.ToString() == "94.23.19.48")
  27.                     {
  28.                         Console.Write(Encoding.UTF8.GetString(receivedata));
  29.                         parseResponse(receivedata);
  30.                         if (Encoding.UTF8.GetString(receivedata).Contains("EOT"))
  31.                             break;
  32.                     }
  33.                 }
  34.             }
  35.             catch (Exception e)
  36.             {
  37.                 Console.WriteLine(e.ToString());
  38.             }
  39.             Console.ReadLine();
  40.         }
  41.  
  42.         static void parseResponse(byte[] data)
  43.         {
  44.             var strData = Encoding.UTF7.GetString(data).Substring(4).Split('\\');
  45.             for (int i = 0; i < strData.Length; i++)
  46.             {
  47.                 if (strData[i].Contains("serverresponse"))
  48.                     continue;
  49.                 else if (strData[i].Contains("EOT"))
  50.                     break;
  51.                 else
  52.                 {
  53.                     var ip = new int[6];
  54.                     var port = 0;
  55.                     if (strData[i] != "")
  56.                     {
  57.                         if (strData[i].Length == 6)
  58.                         {
  59.                             for(int h = 0; h < strData[i].Length; h++)
  60.                                 ip[h] = (int)strData[i][h];
  61.                             port = (256 * ip[4] + ip[5]);
  62.                             Console.WriteLine("{0}.{1}.{2}.{3}:{4}", ip[0], ip[1], ip[2], ip[3], port);
  63.                         }
  64.                     }
  65.                     if (strData[i] == "")
  66.                         strData[i + 1] = "\\" + strData[i + 1];
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement