AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Sockets and webservers (https://forums.alliedmods.net/showthread.php?t=41913)

Alka 11-26-2007 11:40

Re: Sockets and webservers
 
Yeah, get the server uptime, then save it to sql, then make a simple .php script to get it on webpage.

hackziner 11-27-2007 02:27

Re: Sockets and webservers
 
Of course.

You can find a way to get the uptime here:
http://forums.alliedmods.net/showthr...ghlight=uptime

and you can create a webserver with:
http://forums.alliedmods.net/showthread.php?t=63512 ( check sample 1 )

VMAN 01-25-2008 03:57

Re: Sockets and webservers
 
My clan runs this on all their servers

Code:

#include <amxmodx>
#include <fakemeta>

new amx_gamename;
public plugin_init( ) {
        register_plugin( "Game Namer", "1.1", "NeuroToxin" );
        amx_gamename = register_cvar( "amx_gamename", "Team Fortress Classic" );
        register_forward( FM_GetGameDescription, "GameDesc" );
}
 
public GameDesc( ) {
        static gamename[32];
        get_pcvar_string( amx_gamename, gamename, 31 );
        forward_return( FMV_STRING, gamename );
        return FMRES_SUPERCEDE;
}

Sometimes we change domains or forums, is their a way to make this plugin retrieve the value of amx_gamename from a web page?

413-X- 04-25-2008 05:06

Re: Sockets and webservers
 
Great Tutorial :D ! But how to use sockets with an IRC-Bot ???

endeffects 06-10-2008 09:51

Re: Sockets and webservers
 
hallo,

i try to compile the example but i'm getting alot of errors:

Quote:

/home/groups/amxmodx/tmp3/textW7wZxC.sma(24) : error 017: undefined symbol "write_web"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(25) : error 017: undefined symbol "read_web"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(38) : warning 217: loose indentation
/home/groups/amxmodx/tmp3/textW7wZxC.sma(38) : error 029: invalid expression, assumed zero
/home/groups/amxmodx/tmp3/textW7wZxC.sma(38) : error 017: undefined symbol "read_web"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(47) : error 017: undefined symbol "ExplodeString"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(60) : error 017: undefined symbol "disconnect_web"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : warning 217: loose indentation
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : error 029: invalid expression, assumed zero
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : error 017: undefined symbol "write_web"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : warning 215: expression has no effect
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : error 001: expected token: ";", but found "]"
/home/groups/amxmodx/tmp3/textW7wZxC.sma(63) : fatal error 107: too many error messages on one line

Code:

#include <amxmodx>
#include <sockets>
new g_sckweb //socket "id"
#define SCRIPT_NAME "/myplugin/parser.php"
#define REMOTE_HOST "myserver.com" //port d.80
public plugin_init()
{
    register_plugin("Socket sample", "??" ,"Darksnow")
    set_task(5.0,"connect_web")
}
public connect_web()
{
    new error = 0
    new constring[512]
    g_sckweb = socket_open(REMOTE_HOST, 80, SOCKET_TCP, error)
    if (g_sckweb > 0)
    {
        format(constring,511,"GET %s HTTP/1.1^nHost: %s^n^n",SCRIPT_NAME,REMOTE_HOST)
        write_web(constring)
        read_web()
    }
    else
    {
    switch (error)
    {
        case 1: { server_print("Error creating socket"); }
        case 2: { server_print("Error resolving remote hostname"); }
        case 3: { server_print("Error connecting socket"); }
    }
    return PLUGIN_CONTINUE
}
public read_web()
{
    const SIZE = 63
    new line_variable[SIZE + 1], line_value[SIZE + 1]
    if (socket_change(g_sckweb, 100))
    {
    new buf[512], lines[30][100], count = 0
    socket_recv(g_sckweb, buf, 511)
    count = ExplodeString(lines, 50, 119, buf, 13)
    for(new i=0;i<count;i++)
    {
        parse(lines[i], line_variable, SIZE, line_value, SIZE)
        if (equal(line_variable, "some_value"))
        {
            server_print("Value is %s", line_value)
        }
    } 
    if (g_sckweb != 0)
        set_task(0.5, "read_web")
    else
        disconnect_web()
}
public write_web(text[512])
{
    socket_send(g_sckweb, text, 511)
}
public disconnect_web()
{
    server_print("Socket disconnected")
}
stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter ) { // Function by xeroblood
    new nIdx = 0, l = strlen(p_szInput)
    new nLen = (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter ))
    while( (nLen < l) && (++nIdx < p_nMax) )
        nLen += (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter ))
    return nIdx
}

any ideas to get it working?

Greenberet 06-10-2008 19:33

Re: Sockets and webservers
 
download it locally and not with the webcompiler

xOR 01-06-2009 08:59

Re: Sockets and webservers
 
it's not the web compiler, it's the "else" in connect_web() having its closing bracket missing.

EDIT:
oh, and the closing bracket for if (socket_change(g_sckweb, 100)) is missing also. and an include for <amxmodx> before the <sockets> include is needed as well. let me just post the code that compiles:
Code:
#include <amxmodx> #include <sockets> new g_sckweb //socket "id" #define SCRIPT_NAME "/myplugin/parser.php" #define REMOTE_HOST "myserver.com" //port d.80 public plugin_init() {     register_plugin("Socket sample", "??" ,"Darksnow")     set_task(5.0,"connect_web") } public connect_web() {     new error = 0     new constring[512]     g_sckweb = socket_open(REMOTE_HOST, 80, SOCKET_TCP, error)     if (g_sckweb > 0)     {         format(constring,511,"GET %s HTTP/1.1^nHost: %s^n^n",SCRIPT_NAME,REMOTE_HOST)         write_web(constring)         read_web()     }     else     {         switch (error)         {             case 1: { server_print("Error creating socket"); }             case 2: { server_print("Error resolving remote hostname"); }             case 3: { server_print("Error connecting socket"); }         }     }     return PLUGIN_CONTINUE } public read_web() {     const SIZE = 63     new line_variable[SIZE + 1], line_value[SIZE + 1]     if (socket_change(g_sckweb, 100))     {         new buf[512], lines[30][100], count = 0         socket_recv(g_sckweb, buf, 511)         count = ExplodeString(lines, 50, 119, buf, 13)         for(new i=0;i<count;i++)         {             parse(lines[i], line_variable, SIZE, line_value, SIZE)             if (equal(line_variable, "some_value"))             {                 server_print("Value is %s", line_value)             }         }       }     if (g_sckweb != 0)         set_task(0.5, "read_web")     else         disconnect_web() } public write_web(text[512]) {     socket_send(g_sckweb, text, 511) } public disconnect_web() {     server_print("Socket disconnected") } stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter ) { // Function by xeroblood     new nIdx = 0, l = strlen(p_szInput)     new nLen = (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter ))     while( (nLen < l) && (++nIdx < p_nMax) )         nLen += (1 + copyc( p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter ))     return nIdx }

portocala 08-06-2010 05:28

Re: Sockets and webservers
 
One question about the first sockets sample.

Why don't we need to call socket_close ?

Clauu 05-06-2011 04:46

Re: Sockets and webservers
 
Can someone please give a working example about how to retrive some data from a .php file using sockets?

ish12321 01-07-2018 17:18

Re: Sockets and webservers
 
https://forums.alliedmods.net/showthread.php?t=304139


All times are GMT -4. The time now is 14:11.

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