This will handle if the server doesn't respond and parsing is simplified.
PHP Code:
#include <amxmodx>
#include <sockets>
new const Version[] = "0.1";
const TASK_GETIP = 3467;
new g_szServerIP[ 16 ];
public plugin_init()
{
register_plugin( "Get Internet IP" , Version , "bugsy" );
register_clcmd( "say /ip" , "GetIP" );
}
public GetIP( id )
{
if ( g_szServerIP[ 0 ] )
{
client_print( id , print_chat , "* Server IP is [ %s ]" , g_szServerIP );
return;
}
new iSocket , iError;
iSocket = socket_open( "checkip.dyndns.com" , 80 , SOCKET_TCP , iError );
if ( !iSocket || iError )
{
client_print( id , print_chat , "* Error creating socket, unable to obtain IP" );
return;
}
socket_send( iSocket , "GET / HTTP/1.1^nHost: checkip.dyndns.com^n^n" , 42 );
new iData[ 3 ];
iData[ 0 ] = iSocket;
iData[ 1 ] = id;
iData[ 2 ] = get_systime();
set_task( 0.2 , "RecvData" , TASK_GETIP , iData , sizeof( iData ) , "b" );
}
public RecvData( iData[ 3 ] )
{
static szData[ 256 ] , iStartPos , iEndPos;
new iSocket = iData[ 0 ];
new iRequestPlayer = iData[ 1 ];
new iRequestTime = iData[ 2 ];
if ( socket_change( iSocket ) )
{
socket_recv( iSocket , szData , charsmax( szData ) );
iStartPos = strfind( szData , "Current IP Address: " );
if ( iStartPos != -1 )
{
iEndPos = strfind( szData , "</body>" );
copy( g_szServerIP , iEndPos - ( iStartPos + 20 ) , szData[ iStartPos + 20 ] );
socket_close( iSocket );
remove_task( TASK_GETIP );
client_print( iRequestPlayer , print_chat , "* Server IP is [ %s ]" , g_szServerIP );
}
}
if ( ( get_systime() - iRequestTime ) >= 5 )
{
socket_close( iSocket );
remove_task( TASK_GETIP );
client_print( iRequestPlayer , print_chat , "* checkip.dyndns.com failed to respond, unable to obtain IP" );
}
}
__________________