Raised This Month: $ Target: $400
 0% 

socket - server a get server b info


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
biscuit628
Senior Member
Join Date: Jun 2007
Location: 香港HongKong
Old 09-29-2009 , 14:27   socket - server a get server b info
Reply With Quote #1

can someone show a simple code for me
like playing map/playernum/maxplayer..etc
i have look xredirect code
but i don't understand..


i know http://developer.valvesoftware.com/w...quest_format_3
but i don't know how to use in code
__________________
My Plugins

C4man with fun

Sniper Skill bonus
-------------------------
Sorry for my poor English!

Last edited by biscuit628; 09-29-2009 at 14:30.
biscuit628 is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-29-2009 , 14:47   Re: socket - server a get server b info
Reply With Quote #2

PHP Code:
#include <amxmodx>
#include <sockets>
#include <geoip>
#include <regex>

// A2S_INFO definitions for source according to http://developer.valvesoftware.com/wiki/Server_Queries#Source_servers_2
#define A2S_INFO_SOURCE_REPLY_FORMAT "411ssss21111111s"
#define A2S_INFO_SOURCE_IDX_HEADER            0    // Should be FF FF FF FF
#define A2S_INFO_SOURCE_IDX_TYPE            1    // Should be equal to 'I' (0x49)
#define A2S_INFO_SOURCE_IDX_VERSION            2    // Network version. 0x07 is the current Steam version.
#define A2S_INFO_SOURCE_IDX_SERVERNAME        3    // The Source server's name
#define A2S_INFO_SOURCE_IDX_MAP                4    // The current map being played, eg: "de_dust"
#define A2S_INFO_SOURCE_IDX_GAMEDIR            5    // The name of the folder containing the game files, eg: "cstrike"
#define A2S_INFO_SOURCE_IDX_GAMEDESC        6    // A friendly string name for the game type, eg: "Counter Strike: Source"
#define A2S_INFO_SOURCE_IDX_APPID            7    // Steam Application ID
#define A2S_INFO_SOURCE_IDX_NUMPLAYERS        8    // The number of players currently on the server
#define A2S_INFO_SOURCE_IDX_MAXPLAYERS        9    // Maximum allowed players for the server
#define A2S_INFO_SOURCE_IDX_NUMBOTS            10    // Number of bot players currently on the server
#define A2S_INFO_SOURCE_IDX_DEDICATED        11    // 'l' for listen, 'd' for dedicated, 'p' for SourceTV
#define A2S_INFO_SOURCE_IDX_OS                12    // Host operating system. 'l' for Linux, 'w' for Windows
#define A2S_INFO_SOURCE_IDX_PASSWORD        13    // If set to 0x01, a password is required to join this server
#define A2S_INFO_SOURCE_IDX_SECURE            14    // if set to 0x01, this server is VAC secured
#define A2S_INFO_SOURCE_IDX_GAMEVERSION        15    // The version of the game, eg: "1.0.0.22"

// A2S_INFO definitions for goldsource according to http://developer.valvesoftware.com/wiki/Server_Queries#Goldsource_servers_2
#define A2S_INFO_GOLD_REPLY_FORMAT            "41sssss111111[ss14411]11"
#define A2S_INFO_GOLD_IDX_HEADER            0    // Should be FF FF FF FF
#define A2S_INFO_GOLD_IDX_TYPE                1     // Should be equal to 'm' (0x6D) - for older servers it's 'C' (0x43)
#define A2S_INFO_GOLD_IDX_IP                2    // Game Server IP address and port
#define A2S_INFO_GOLD_IDX_SERVERNAME        3    // The server's name
#define A2S_INFO_GOLD_IDX_MAP                4    // The current map being played, eg: "de_dust"
#define A2S_INFO_GOLD_IDX_GAMEDIR            5    // The name of the folder containing the game files, eg: "cstrike"
#define A2S_INFO_GOLD_IDX_GAMEDESC            6    // A friendly string name for the game type, eg: "Counter-Strike"
#define A2S_INFO_GOLD_IDX_NUMPLAYERS        7    // The number of players currently on the server
#define A2S_INFO_GOLD_IDX_MAXPLAYERS        8    // Maximum allowed players for the server
#define A2S_INFO_GOLD_IDX_VERSION            9    // Network version. 0x07 is the current Steam version.
#define A2S_INFO_GOLD_IDX_DEDICATED            10     // 'l' for listen, 'd' for dedicated, 'p' for HLTV
#define A2S_INFO_GOLD_IDX_OS                11    // Host operating system. 'l' for Linux, 'w' for Windows
#define A2S_INFO_GOLD_IDX_PASSWORD            12    // If set to 0x01, a password is required to join this server
#define A2S_INFO_GOLD_IDX_ISMOD                13    // If set to 0x01, this byte is followed by ModInfo
#define A2S_INFO_GOLD_IDX_SECURE            14    // if set to 0x01, this server is VAC secured
#define A2S_INFO_GOLD_IDX_NUMBOTS            15    // Number of bot players currently on the server
#define A2S_INFO_GOLD_IDX_MOD_URLINFO        14    // URL containing information about this mod
#define A2S_INFO_GOLD_IDX_MOD_URLDL            15    // URL to download this mod
#define A2S_INFO_GOLD_IDX_MOD_NUL            16    // 0x00
#define A2S_INFO_GOLD_IDX_MOD_MODVERSION    17    // Version of the installed mod
#define A2S_INFO_GOLD_IDX_MOD_MODSIZE        18    // The download size of this mod
#define A2S_INFO_GOLD_IDX_MOD_SVONLY        19    // If 1 this is a server side only mod
#define A2S_INFO_GOLD_IDX_MOD_CIDLL            20    // If 1 this mod has a custom client dll
#define A2S_INFO_GOLD_IDX_MOD_SECURE        21    // if set to 0x01, this server is VAC secured
#define A2S_INFO_GOLD_IDX_MOD_NUMBOTS        22    // Number of bot players currently on the server

#define PATTERN "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
#define DELAY   6.0
#define TASK    386

enum _:STR_RETURN {
    
HOSTNAME 0,
    
MAP
};

enum _:INT_RETURN {
    
BOTS 0,
    
PLAYERS,
    
MAX_PLAYERS
};

new 
bool:g_bServerAlive;
new 
Float:g_flCooldown;
new 
bool:g_bIsWindows;
new 
g_strReturnSTR_RETURN ][ 64 ];
new 
g_intReturnINT_RETURN ];
new 
g_szIP32 ];
new 
g_iSocket;

public 
plugin_init( ) {
    
register_plugin"Locate Server""1.0""xPaw" );
    
    
register_clcmd"say""hookSay" );
}

public 
hookSayid ) {
    new 
szSaid40 ], szCmd], szIP32 ];
    
read_argsszSaid39 );
    
remove_quotesszSaid );
    
    if( 
szSaid] != '/' )
        return 
PLUGIN_CONTINUE;
    
    
parseszSaidszCmd7szIP31 );
    
    if( 
equaliszCmd"/locate") || equaliszCmd"/ip") ) {
        new 
szError64 ], iReturnRegex:iResult regex_matchszIPPATTERNiReturnszError63 );
        
        switch( 
iResult ) {
            case 
REGEX_MATCH_FAILREGEX_PATTERN_FAIL: return PLUGIN_HANDLED;
            case 
REGEX_NO_MATCH: {
                
GreenPrintid"^4[LOCATE]^1 Wrong IP Address:^3 %s"szIP );
                
                return 
PLUGIN_HANDLED;
            }
            default: {
                new 
szPort];
                
strtokszIPszIP31szPort5':' );
                
                if( !
szPort] ) {
                    
GreenPrintid"^4[LOCATE]^1 Put a port!" );
                    
                    return 
PLUGIN_HANDLED;
                }
                
                new 
Float:flGametime get_gametime( );
                
                if( 
g_flCooldown flGametime ) {
                    
g_flCooldown flGametime DELAY;
                    
                    
GreenPrintid"^4[LOCATE]^1 Locating:^3 %s:%s"szIPszPort );
                    
                    if( 
ConnectServeridszIPstr_to_numszPort ) ) ) {
                        
SendRequest( );
                        
                        
set_task0.5"ShowResults" );
                    }
                    
                    
regex_freeiResult );
                } else {
                    new 
iSeconds floatroundg_flCooldown flGametime )
                    
GreenPrintid"^4[LOCATE]^1 Please wait^4 %i^1 second%s!"iSeconds == "" "s" );
                }
                
                return 
PLUGIN_HANDLED;
            }
        }
    }
    
    return 
PLUGIN_CONTINUE;
}

public 
ShowResults( ) {
    
remove_taskTASK );
    
    new 
szCountry46 ];
    
geoip_countryg_szIPszCountry45 );
    
    
GreenPrint0"^4[LOCATE] IP:^1 %s^4 | Country:^1 %s"g_szIPszCountry );
    
    if( 
g_bServerAlive ) {
        new 
szBots16 ];
        
        if( 
g_intReturnBOTS ] > )
            
formatexszBots15"^1 (^3%i^1 bots)"g_intReturnBOTS ] );
        
        
GreenPrint0"^4[LOCATE]^1 %s^4 | OS:^1 %s^4 | Map:^1 %s^4 | Players:^3 %i^1 /^3 %i%s"g_strReturnHOSTNAME ], g_bIsWindows "Windows" "Linux",
            
g_strReturnMAP ], g_intReturnPLAYERS ], g_intReturnMAX_PLAYERS ], szBots );
    } else
        
GreenPrint0"^4[LOCATE]^1 This is not Half-Life Server!" );
    
    
DisconnectServer( );
}

public 
SendRequest( ) {
    new 
szOldRequest12 ], szNewRequest26 ], i;
    
formatszOldRequest11"%c%c%c%c%s"255255255255"details" );
    
formatszNewRequest25"%c%c%c%c%c%s%c"25525525525584"Source Engine Query");
    
    
// Clear infos
    
g_bServerAlive false;
    
g_bIsWindows false;
    
g_strReturnHOSTNAME    ][ ] = '^0';
    
g_strReturnMAP         ][ ] = '^0';
    
g_intReturnBOTS        ]      = 0;
    
g_intReturnPLAYERS     ]      = 0;
    
g_intReturnMAX_PLAYERS ]      = 0;
    
    
// Send sockets
    
for( = -14i++ )
        
socket_send2g_iSocketszOldRequest11 );
    
    for( 
= -14i++ )
        
socket_send2g_iSocketszNewRequest25 );
    
    
set_task0.1"ReceiveInfo"TASK__"b" );
    
    return 
PLUGIN_CONTINUE;
}

public 
ReceiveInfo( ) {
    if( 
socket_changeg_iSocket) ) {
        new 
szBuffer1400 ], iReceivedLen;
        
        
iReceivedLen socket_recvg_iSocketszBuffer1400 );
        
        if( 
iReceivedLen ) { // shortest reply is a ping response with length of 6
            
g_bServerAlive true;
            
            if( 
equalszBuffer, { -1, -1, -1, -}, ) ) {
                new 
aIndexes100 ], szOs];
                
                switch( 
szBuffer] ) {
                    case 
'm': {
                        
index_createszBufferiReceivedLenA2S_INFO_GOLD_REPLY_FORMATaIndexes )
                        
                        
copycg_strReturnHOSTNAME ], 63szBufferaIndexesA2S_INFO_GOLD_IDX_SERVERNAME ] ], );
                        
copycg_strReturnMAP ], 63szBufferaIndexesA2S_INFO_GOLD_IDX_MAP ] ], );
                        
copycszOs2szBufferaIndexesA2S_INFO_GOLD_IDX_OS ] ], );
                        
                        
g_intReturnBOTS ] = szBufferaIndexesA2S_INFO_GOLD_IDX_NUMBOTS ] ];
                        
g_intReturnPLAYERS ] = szBufferaIndexesA2S_INFO_GOLD_IDX_NUMPLAYERS ] ];
                        
g_intReturnMAX_PLAYERS ] = szBufferaIndexesA2S_INFO_GOLD_IDX_MAXPLAYERS ] ];
                    }
                    case 
'I': {
                        
index_createszBufferiReceivedLenA2S_INFO_SOURCE_REPLY_FORMATaIndexes )
                        
                        
copycg_strReturnHOSTNAME ], 63szBufferaIndexesA2S_INFO_SOURCE_IDX_SERVERNAME ] ], );
                        
copycg_strReturnMAP ], 63szBufferaIndexesA2S_INFO_SOURCE_IDX_MAP ] ], );
                        
copycszOs2szBufferaIndexesA2S_INFO_SOURCE_IDX_OS ] ], );
                        
                        
g_intReturnBOTS ] = szBufferaIndexesA2S_INFO_SOURCE_IDX_NUMBOTS ] ];
                        
g_intReturnPLAYERS ] = szBufferaIndexesA2S_INFO_SOURCE_IDX_NUMPLAYERS ] ];
                        
g_intReturnMAX_PLAYERS ] = szBufferaIndexesA2S_INFO_SOURCE_IDX_MAXPLAYERS ] ];
                    }
                }
                
                if( 
szOs] == 'w' )
                    
g_bIsWindows true;
            }
        }
    }
}

public 
bool:ConnectServeridszAddress[ ], iPort ) {
    if( 
g_iSocket )
        return 
false;
    
    new 
iError;
    
    
g_iSocket socket_openszAddressiPortSOCKET_UDPiError );
    
    if( 
g_iSocket <= || iError ) {
        new 
szError40 ];
        
        switch( 
iError ) {
            case 
0szError "No Error";
            case 
1szError "Error while creating socket";
            case 
2szError "Couldn't resolve hostname";
            case 
3szError "Couldn't connect to given hostname:port";
        }
        
        
GreenPrintid"^4[LOCATE]^1 %s (^3 %s:%i ^1)"szErrorszAddressiPort );
        
        return 
false;
    }
    
    
copyg_szIP31szAddress );
    
    return 
true;
}

public 
DisconnectServer( ) {
    
socket_closeg_iSocket );
    
g_iSocket 0;
}

public 
index_createsData1400 ], nDataLensFormatString100 ], aIndexes100 ] ) {
    new 
nFormatPosnIndexPosnDataIndexnFormatPosMax strlensFormatString );
    
    while ((
nIndexPos nFormatPosMax) && (nDataIndex <= nDataLen)) {
        switch (
sFormatString[nFormatPos]) {
            case 
'1': {
                
aIndexes[nIndexPos] = nDataIndex
                nDataIndex
++
                
nIndexPos++
            }
            case 
'2': {
                
aIndexes[nIndexPos] = nDataIndex
                nDataIndex 
+= 2
                nIndexPos
++
            }
            case 
'4': {
                
aIndexes[nIndexPos] = nDataIndex
                nDataIndex 
+= 4
                nIndexPos
++
            }
            case 
's': {
                
aIndexes[nIndexPos] = nDataIndex
                
do { nDataIndex++; } while ((sData[nDataIndex] != 0) && (nDataIndex nDataLen))
                
nDataIndex++
                
nIndexPos++
            }
            case 
'[': {
                if (
sData[nDataIndex] != 1)
                    do { 
nFormatPos++; } while ((sFormatString[nFormatPos] != ']') && (nFormatPos nFormatPosMax))
                
                
nDataIndex++
                
nIndexPos++
            }
            case 
']'nDataIndex++
            default: 
nDataIndex++
        }
        
        
nFormatPos++
    }
    
    return 
nIndexPos;
}

stock GreenPrintid, const message[], any:... ) {
    new 
szMessage192 ];
    
vformatszMessage191message);
    
    static 
iSayText;
    if( !
iSayText )
        
iSayText get_user_msgid"SayText" );
    
    
message_beginid MSG_ONE_UNRELIABLE MSG_BROADCASTiSayText_id );
    
write_byte);
    
write_stringszMessage );
    
message_end( );
    
    return 
1;

Have fun
__________________
xPaw is offline
biscuit628
Senior Member
Join Date: Jun 2007
Location: 香港HongKong
Old 09-29-2009 , 14:50   Re: socket - server a get server b info
Reply With Quote #3

thanks for fast reply!
+k
__________________
My Plugins

C4man with fun

Sniper Skill bonus
-------------------------
Sorry for my poor English!
biscuit628 is offline
Phant
Veteran Member
Join Date: Sep 2009
Location: New Jersey
Old 11-22-2009 , 02:43   Re: socket - server a get server b info
Reply With Quote #4

How to socket_recv() accept in one string by parts? With cycle/for/while? Need small example, thanks!
Phant is offline
Send a message via ICQ to Phant
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode