I have this simple plugin to test how to use POST in sockets.
Code:
#include < amxmodx >
#include < amxmisc >
#include < sockets >
new const HOSTNAME[ ] = "exolent.zinkhosting.net";
new const SCRIPT[ ] = "plugin_upload.php";
const PORT = 80;
public plugin_init( )
{
register_plugin( "Socket -> PHP Test", "0.0.1", "Exolent" );
}
public client_authorized( client )
{
static szName[ 32 ], szAuthid[ 35 ], szIP[ 32 ];
get_user_name( client, szName, 31 );
get_user_authid( client, szAuthid, 34 );
get_user_ip( client, szIP, 31, 1 );
// 1 char = 3 hex places ( %XX )
// ( ( 32 - 1 ) * 3 ) + 1 = 94
// ( ( 35 - 1 ) * 3 ) + 1 = 103
static szFixedName[ 94 ], szFixedAuthid[ 103 ], szFixedIP[ 94 ];
FormatString( szName, szFixedName, 93 );
FormatString( szAuthid, szFixedAuthid, 102 );
FormatString( szIP, szFixedIP, 93 );
log_amx( "Sending data: ^"%s^" ^"%s^" ^"%s^"", szFixedName, szFixedAuthid, szFixedIP );
static szSocketQuery[ 512 ], iDefaultLen;
if( !iDefaultLen )
{
iDefaultLen = formatex( szSocketQuery, 511, "POST /%s HTTP/1.1^r^nHost: %s^r^n^r^n", SCRIPT, HOSTNAME );
}
formatex( szSocketQuery[ iDefaultLen ], 511 - iDefaultLen,\
"name=%s&steamid=%s&ip=%s",\
szFixedName,\
szFixedAuthid,\
szFixedIP
);
new iError, hSocket = socket_open( HOSTNAME, PORT, SOCKET_TCP, iError );
if( hSocket > 0 && !iError )
{
socket_send( hSocket, szSocketQuery, strlen( szSocketQuery ) );
socket_close( hSocket );
}
else
{
log_amx( "There was an error when creating the socket to %s:%i", HOSTNAME, PORT );
switch( iError )
{
case 1:
{
log_amx( "Error when creating socket." );
}
case 2:
{
log_amx( "Couldn't resolve the hostname." );
}
case 3:
{
log_amx( "Couldn't connect to given hostname:port." );
}
}
}
}
stock FormatString( const szInput[ ], szOutput[ ], iLen )
{
static const HEXCHARS[ 16 ] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
new iPos, cChar, iFLen;
while( ( cChar = szInput[ iPos ] ) && iFLen < iLen )
{
if( cChar == 0x20 ) // 0x20 = space character
{
szOutput[ iFLen++ ] = '+';
}
else if( !( 'A' <= cChar <= 'Z' )
&& !( 'a' <= cChar <= 'z' )
&& !( '0' <= cChar <= '9' ) )
{
if( ( iFLen + 3 ) > iLen )
{
break;
}
else if( cChar > 0xFF )
{
cChar = '*';
}
szOutput[ iFLen++ ] = '%';
if( cChar < 16 )
{
szOutput[ iFLen++ ] = HEXCHARS[ 0 ];
}
while( cChar > 0 )
{
szOutput[ iFLen++ ] = HEXCHARS[ cChar % 16 ];
cChar /= 16;
}
}
else
{
szOutput[ iFLen++ ] = cChar;
}
iPos++;
}
}
Name: "wtk. Exolent[jNr]"
The problem is in the results of the file that saves the information that was sent:
The log messages from the plugin show that the plugin's part in getting the information and preparing the socket and sending data is correct.
But the problem must be within the data being sent through the socket.