View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-19-2009 , 13:10   Re: Text from a site - printing to console
Reply With Quote #4

This will read the text from a remote txt file and print it to server console and chat. If you have any problems getting it to work with your application let me know.

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

#define BUFFER_SIZE    1024

//Random remote text file
//http://www.isotc211.org/2005/xlink/version.txt

new const g_szHost[] = "www.isotc211.org";
new const 
g_szFile[] = "/2005/xlink/version.txt";
new const 
g_szDataStart[] = "^r^n^r^n";

new 
Float:g_fSocketWait33 ];
new 
g_TimedOut33 ];

public 
plugin_init()
{
    
register_plugin"socket example" "0.1" "bugsy" );
    
    
register_clcmd"say /getdata" "cmd_GetData" );
}


public 
cmd_GetDataid )
{
    new 
iSocket iError;

    
iSocket socket_openg_szHost 80 SOCKET_TCP iError );

    if( ( 
iSocket <= ) || iError )
        return 
PLUGIN_HANDLED;
        
    static 
szPacket[256];

    
g_TimedOutid ] = 0;
    
    
formatexszPacket 255 "GET %s HTTP/1.1^r^nHost: %s^r^nConnection: close^r^n^r^n" g_szFile g_szHost );

    
socket_sendiSocket szPacket strlenszPacket ) );
    
    
g_fSocketWaitid ] = get_gametime();

    new 
param];
    
param] = id;
    
param] = iSocket;
    
    if( !
task_exists5511 id ) )
        
set_task0.1 "CheckRecv" 5511 id param "b" );

    return 
PLUGIN_HANDLED;
}

public 
CheckRecvparam] )
{    
    new 
id param];
    new 
iSocket param];
    
    static 
szDataBUFFER_SIZE ];
    static 
iPos;
    
    if ( 
socket_changeiSocket ) )
    {
        
//Append data to our receive buffer
        
socket_recviSocket szDataiPos ] , BUFFER_SIZE iPos );
        
        
//I noticed sometimes that socket_change was returning true even though no new data was waiting to be read.
        //This will verify if new data is actually received.
        
if ( strlen szDataiPos ] ) )
        {
            
//Set buffer pos to end of received data so the next time we recv data it will be appended
            
iPos strlenszData );
            
            
//Set var to the time at which data last recvd
            
g_fSocketWaitid ] = get_gametime();
        
            
//Here we are looking where the data starts. After the http header there is
            //always a 2 carriage returns.
            
new iDataPos strfindszData g_szDataStart );
            
            
//If datapos was found, we have our data
            
if ( iDataPos != -)
            {
                
server_printszDataiDataPos charsmaxszDataStart ) ] );
                
client_printid print_chat szDataiDataPos charsmaxszDataStart ) ] );
                
                
iPos 0;
                
CloseSocketid iSocket );
            }
        }
    }
    
    
//If we do not get any response from the server after 5 seconds then we close socket
    
if( ( get_gametime() - g_fSocketWaitid ] ) >= 7.0 )    
    {    
        
client_printid print_chat "* Server took too long to respond" );
        
iPos 0;
        
CloseSocketid iSocket );
    }
    
    return 
PLUGIN_HANDLED;
}

public 
CloseSocketid iSocket )
{
    
remove_task5511 id );
    
socket_closeiSocket );

__________________

Last edited by Bugsy; 12-19-2009 at 21:09.
Bugsy is offline