provide the basic socket functions: open, send, recv, close
web_get a example script to load a file from internet
type
web_get www.google.de /
Code: Select all
/*
*
* AMX Mod X Script
* Downloadscript
*
*/
#include <amxmodx>
#include <amxmisc>
#include <sockets>
new socket
static const s_log[] = "web_get.log"
static const s_data[] = "web_get.txt"
public plugin_init()
{
register_plugin("Sockets: Web Get","0.1","SniperBeamer")
register_concmd("web_get","send_string",ADMIN_LEVEL_A,"<hostname> <website>")
}
public connect_to(hostname[])
{
new error
socket = socket_open(hostname,80,SOCKET_TCP,error)
if ( socket<=0 )
{
log_amx("Connecting to %s... Cannot open socket",hostname)
return false
}
log_amx("Connecting to %s... OK",hostname)
return true
}
public send_string(id,level,cid)
{
if ( !cmd_access(id,level,cid,2) )
return PLUGIN_HANDLED
new arg1[32],arg2[64],temp[128]
read_argv(1,arg1,31)
read_argv(2,arg2,63)
delete_file(s_data)
if ( !connect_to(arg1) )
{
format(temp,127,"Connecting to %s... Cannot open socket",arg1)
show_motd(id,temp,"Error")
return PLUGIN_HANDLED
}
new file[128]
format(file,127,"GET %s HTTP/1.1^nhost: %s^n^n",arg2, arg1)
socket_send(socket, file, 0)
if ( socket_change(socket,1000000) )
{
new site[1024],recv
do
{
if(!socket_change(socket, 100000)) break
recv = socket_recv(socket,site,1023)
write_file(s_data,site,-1)
} while ( recv>0 )
if ( file_size(s_data,0)>3072 )
{
format(temp,127,"Requesting %s from %s... File too big",arg2,arg1)
show_motd(id,temp,"Error")
log_to_file(s_log,temp)
}
else
{
format(temp,127,"%s%s",arg1,arg2)
show_motd(id,s_data,temp)
log_amx("Requesting %s from %s... OK",arg2,arg1)
}
}
else
{
format(temp,127,"Requesting %s from %s... No answer",arg2,arg1)
show_motd(id,temp,"Error")
log_to_file(s_log,temp)
}
socket_close(socket)
return PLUGIN_HANDLED
}- Server is timeout, if the file load
Files might not work
