Sockety

Prosty przykład jak używać socketów z poziomu pluginu amxx
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <amxmodx>
#include <amxmisc>
#include <sockets></sockets></amxmisc></amxmodx>
#define SITE "/index.php/2011/09/12/info-po-smierci/6"
#define HOST "darkgl.pl"
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "DarkGL"
new g_Socket;
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_clcmd("say /test","test")
}
public test(id){
new iError,szSendBuffer[512]
g_Socket = socket_open(HOST, 80, SOCKET_TCP, iError)
switch (iError)
{
case 1:
{
log_amx("Unable to create socket.")
return ;
}
case 2:
{
log_amx("Unable to connect to hostname.")
return ;
}
case 3:
{
log_amx("Unable to connect to the HTTP port.")
return ;
}
}
format(szSendBuffer, charsmax(szSendBuffer), "GET %s^nHost:%s^r^n^r^n", SITE, HOST)
socket_send(g_Socket, szSendBuffer, charsmax(szSendBuffer))
set_task(1.0, "socketAnswer", .flags = "b")
}
public socketAnswer(){
if (socket_change(g_Socket)) {
new szData[1024]
socket_recv(g_Socket, szData, charsmax(szData) )
log_amx(szData);
socket_close(g_Socket);
remove_task( 0 );
}
}
#include <amxmodx> #include <amxmisc> #include <sockets></sockets></amxmisc></amxmodx> #define SITE "/index.php/2011/09/12/info-po-smierci/6" #define HOST "darkgl.pl" #define PLUGIN "New Plug-In" #define VERSION "1.0" #define AUTHOR "DarkGL" new g_Socket; public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) register_clcmd("say /test","test") } public test(id){ new iError,szSendBuffer[512] g_Socket = socket_open(HOST, 80, SOCKET_TCP, iError) switch (iError) { case 1: { log_amx("Unable to create socket.") return ; } case 2: { log_amx("Unable to connect to hostname.") return ; } case 3: { log_amx("Unable to connect to the HTTP port.") return ; } } format(szSendBuffer, charsmax(szSendBuffer), "GET %s^nHost:%s^r^n^r^n", SITE, HOST) socket_send(g_Socket, szSendBuffer, charsmax(szSendBuffer)) set_task(1.0, "socketAnswer", .flags = "b") } public socketAnswer(){ if (socket_change(g_Socket)) { new szData[1024] socket_recv(g_Socket, szData, charsmax(szData) ) log_amx(szData); socket_close(g_Socket); remove_task( 0 ); } }
#include 
#include 
#include 

#define SITE                "/index.php/2011/09/12/info-po-smierci/6"
#define HOST                    "darkgl.pl"

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "DarkGL"

new g_Socket;

public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)

register_clcmd("say /test","test")
}

public test(id){
new iError,szSendBuffer[512]

g_Socket = socket_open(HOST, 80, SOCKET_TCP, iError)

switch (iError)
{
case 1:
{
log_amx("Unable to create socket.")
return ;
}
case 2:
{
log_amx("Unable to connect to hostname.")
return ;
}
case 3:
{
log_amx("Unable to connect to the HTTP port.")
return ;
}
}

format(szSendBuffer, charsmax(szSendBuffer), "GET %s^nHost:%s^r^n^r^n", SITE, HOST)
socket_send(g_Socket, szSendBuffer, charsmax(szSendBuffer))

set_task(1.0, "socketAnswer", .flags = "b")
}

public socketAnswer(){
if (socket_change(g_Socket)) {
new szData[1024]

socket_recv(g_Socket, szData, charsmax(szData) )

log_amx(szData);

socket_close(g_Socket);

remove_task( 0 );
}
}
Pobiera on kod html strony http://www.darkgl.pl/index.php/2011/09/12/info-po-smierci/6 oczywiście socketów można używać do jeszcze wielu innych rzeczy (wymienianie informacji między serwerami itp.) A tutaj linki do tutoriali (po angielsku) http://forums.alliedmods.net/showthread.php?t=41913 http://forums.alliedmods.net/showthread.php?t=151401 http://forums.alliedmods.net/showthread.php?t=142858 http://forums.alliedmods.net/showthread.php?t=150519

3 komentarzy o “Sockety

  1. Fajnie, dziękować, ale mam taki kod, do którego sam doszedłem i nie najlepiej to działa. Raz pobiera stronę, raz tylko kilka pierwszych znaków, nie wiem dlaczego.

Dodaj komentarz

This site uses Akismet to reduce spam. Learn how your comment data is processed.