Click here to Skip to main content
Click here to Skip to main content

Game Server Scanner

, 22 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Port scanner to find game servers on a LAN.

Screenshot

Sample Image - gfxscan.jpg

Introduction

gfxscan is a simple port scanner developed to search for remote computers on LAN running game servers.

It sends simple UDP request asynchronously to all the IPSs on a particular port in a subnet. The port being 27015 in case of counter strike. The response is then parsed to get the details of the server. There are many additional requests which can be made to know more about the game, which I will be posting later.

I tried to find a simple port scanner for LAN and Googled a lot, but didn't find any. So I decided to write my own game server scanner.

Problems Faced

Major problem was to reduce the time wait for a read call. I got rid of it by setting option System::Net::Sockets::SocketOptionName::ReceiveTimeout as 1000.

sock->SetSocketOption(System::Net::Sockets::SocketOptionLevel::Socket, 
      System::Net::Sockets::SocketOptionName::ReceiveTimeout,1000);

The blocking nature of a socket can be nulled using sock->set_Blocking(false).

Major Code Fragments

  1. Creating a UDP socket, starting a new thread to read for the response from hosts if found a server, and sending UDP requests to hosts in a subnet:
    void scanServers(String* ip[],int prt,int count)
    {
      boolRead =1;
      port = prt;
      sock = new Socket(AddressFamily::InterNetwork, 
                 SocketType::Dgram,ProtocolType::Udp);
      readThread = new Thread(new ThreadStart(this,read));
      readThread->Start();
      Byte msg[]= System::Text::Encoding::ASCII->GetBytes("xxxxinfo\0");
      Byte msg2[]= new Byte[1000];
      msg[0] = 0xff;
      msg[1] = 0xff;
      msg[2] = 0xff;
      msg[3] = 0xff;
      for(int i=0;i<count;i++)
      {
        IPAddress* ipAdd = IPAddress::Parse(ip->GetValue(i)->ToString());
        IPEndPoint* endPoint = new IPEndPoint(ipAdd,port);
        EndPoint* senderRemote = __try_cast<EndPoint*>(endPoint);
        try
        {
          sock->SendTo(msg, 0, msg->Length, SocketFlags::None, endPoint);
        }
        catch ( SocketException* se )
        {
          //MessageBox::Show(se->Message,"exception while sending",
               MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
        }
      }
    }
  2. Setting the non-blocking and time wait options, reading from hosts on the same socket, decoding the message, and then sending it to display the message correctly:
    void read()
    {
      IPEndPoint* sender = new IPEndPoint(IPAddress::Any, port);
      EndPoint* senderRemote = __try_cast<EndPoint*>(sender);
      Byte msg2[]= new Byte[1000];
      sock->set_Blocking(false);
      sock->SetSocketOption(System::Net::Sockets::SocketOptionLevel::Socket,
               System::Net::Sockets::SocketOptionName::ReceiveTimeout,1000);
    
      while(boolRead==1)
      {
        try 
        {
          int iR = sock->ReceiveFrom(msg2, 0, msg2->Length, 
                        SocketFlags::None, &senderRemote);
          Char chars[] = new Char[2000];
          Decoder* d = Encoding::UTF8->GetDecoder();
          int charLen = d->GetChars(msg2, 0, msg2->Length, chars, 0);
          String* str = new String(chars); 
          parseAndAdd(str);
        }
        catch (SocketException* se)
        {
          MessageBox::Show(se->Message,"exception while sending", 
                  MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
        }
      }
      this->labelStatus->set_Text(new String("Scan Complete."));
      this->labelStatus->Invalidate();
    }

I am looking forward to making a class to provide a neat interface to find game servers on LAN, and to ping them for more information of the game, like no. of players, map being played, and status of each player.

Happy Gaming till then Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

gfx_sikander
Web Developer
United States United States
gfx_sikander aka sam aka Wicked_SuNny. call him by any of these names. Gfx is pusuing B.Tech in computer science and engineering at International institute of information technology hyderabad. Currently, he in 4th year and looking forward to explore .Net Technology and its competitors. He is programming since the age of 14. His area of interest are C++, ASP.Net, Game programming and Compter Networks. He likes to make web pages and flash animations. It must also be mentioned here that gfx does not usually speak about himself in the 3rd person. Smile | :)
 
visit his space to know more about him ...

Comments and Discussions

 
GeneralRe: Game server scanner. PinmemberKingOleg12-Sep-06 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140902.1 | Last Updated 23 Aug 2004
Article Copyright 2004 by gfx_sikander
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid