AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Sockets]: Download a text for server usage (https://forums.alliedmods.net/showthread.php?t=163633)

OFFBEAT 08-01-2011 14:38

[Sockets]: Download a text for server usage
 
I can't seem to get the sockets to work properly, whats wrong with this code (new to socket and serious amxx coding), I don't get any errors and file is created but I don't get hold of the text from the host:

PHP Code:

#include <amxmodx>
#include <sockets>

#define MAX_TEMP 128

new strDemFileMAX_TEMP ], strBufferMAX_TEMP 100 ];

public 
plugin_init( )
{
    
register_plugin"Download text testing""1.0""OFFBEAT" );
    
    
register_clcmd"say /download""Download" );
    
    new 
strTempMAX_TEMP ];
    
get_localinfo"amxx_datadir"strTempMAX_TEMP );
    
formatstrTempMAX_TEMP 1"%s/Downloads"strTemp );
    
    if( !
dir_existsstrTemp ) )
        
mkdirstrTemp );
    
    
formatexstrDemFileMAX_TEMP 1"%s/dled_text.txt"strTemp );
}

public 
Download( )
{
    if( 
file_existsstrDemFile ) )
        
delete_filestrDemFile );
    
    new 
strHostMAX_TEMP ] = "kz-scandinavia.com";
    new 
strFileMAX_TEMP ] = "demos_se.txt";
    new 
error 0strMessageMAX_TEMP ];
    
    new 
error_socket socket_openstrHost80SOCKET_TCPerror );
    
    
formatexstrMessage, ( MAX_TEMP ) - 1"GET /%s HTTP/1.1^nHost: %s^r^n^r^n"strFilestrHost );
    
    
socket_senderror_socketstrMessage, ( MAX_TEMP ) -);
    
    
set_task0.2"Recieve"error_socket );
    
    return 
PLUGIN_CONTINUE;
}

public 
Recieve( const Socket )
{
    
socket_recvSocketstrBuffer, ( MAX_TEMP 100 ) - );
    
    new 
file fopenstrDemFile"at" );
    
fputsfilestrBuffer );
    
fclosefile );
    
    
server_printstrDemFile );
    
    return 
PLUGIN_CONTINUE;



AoD90 08-01-2011 15:04

Re: [Sockets]: Download a text for server usage
 
socket_open( "kz-scandinavia.com", 80, SOCKET_TCP, error );

OFFBEAT 08-01-2011 16:06

Re: [Sockets]: Download a text for server usage
 
AoD90: I wrote the wrong code there :p suppose to be
PHP Code:

socket_openstrHost80SOCKET_TCPerror ); 

my bad. Still can't get the text though.

xPaw 08-01-2011 20:15

Re: [Sockets]: Download a text for server usage
 
Code:
    new error_socket = socket_open( strHost, 80, SOCKET_TCP, error );         if( error_socket > 0 )     {         server_print( "Error openening socket to host!" );         return 0;     }
This is no sense. Go look how sockets work, search for examples.

Bugsy 08-01-2011 20:37

Re: [Sockets]: Download a text for server usage
 
Take a look at this

http://forums.alliedmods.net/showpos...85&postcount=5

OFFBEAT 08-01-2011 20:48

Re: [Sockets]: Download a text for server usage
 
I got it successfully to work BUT the host gave me this like if I was in the wrong link.

PHP Code:

HTTP/1.1 301 Moved Permanently

Date
Tue02 Aug 2011 00:41:32 GMT

Server
Apache

Location
http://www.kz-scandinavia.com/demos_se.txt

VaryAccept-Encoding

Content
-Length250

Content
-Typetext/htmlcharset=iso-8859-1



<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<
html><head>
<
title>301 Moved Permanently</title>
</
head><body>
<
h1>Moved Permanently</h1>
<
p>The document has moved <a href="http://www.kz-scandinavia.com/demos_se.txt">here</a>.</p>
</
body></html


Bugsy 08-01-2011 21:15

Re: [Sockets]: Download a text for server usage
 
Show your HTTP request packet

OFFBEAT 08-01-2011 21:18

Re: [Sockets]: Download a text for server usage
 
This is the code I have now:
PHP Code:

#include <amxmodx>
#include <sockets>

#define MAX_TEMP 128
#define TASK 19

new strDemFileMAX_TEMP ], strBufferMAX_TEMP 100 ], i_Socket;

public 
plugin_init( )
{
    
register_plugin"Download text testing""1.0""OFFBEAT" );
    
    
register_clcmd"say /download""Download" );
    
    new 
strTempMAX_TEMP ];
    
get_localinfo"amxx_datadir"strTempMAX_TEMP );
    
formatstrTempMAX_TEMP 1"%s/Downloads"strTemp );
    
    if( !
dir_existsstrTemp ) )
        
mkdirstrTemp );
    
    
formatexstrDemFileMAX_TEMP 1"%s/dled_text.txt"strTemp );
}

public 
Download( )
{
    if( 
file_existsstrDemFile ) )
        
delete_filestrDemFile );
    
    new 
strHostMAX_TEMP ] = "kz-scandinavia.com";
    new 
strFileMAX_TEMP ] = "demos_se.txt";
    new 
strMessageMAX_TEMP ];
    static 
error;
    
    
i_Socket socket_openstrHost80SOCKET_TCPerror );
    
    
formatexstrMessagecharsmaxstrMessage ), "GET /%s HTTP/1.1^nHost: %s^r^n^r^n"strFilestrHost );
    
    
socket_sendi_SocketstrMessagesizeofstrMessage ) );
    
    
set_task0.2"Recieve"TASK__"b" );
}

public 
Recieve( )
{
    if( 
socket_changei_Socket ) )
    {
        
socket_recvi_SocketstrBuffersizeofstrBuffer ) );
    
        new 
file fopenstrDemFile"at" );
        
fputsfilestrBuffer );
        
fclosefile );
    
        
server_printstrDemFile );
        
        
remove_taskTASK );
    }


and that text i showed is what i got back from the host when requesting the txt file content. (I notice i always get 301 code even which link i type in after .com/ whatever it exists or not)

Bugsy 08-01-2011 21:35

Re: [Sockets]: Download a text for server usage
 
Well your first step in debugging is to get a successful response. Have you tried to retrieve a file on a different server? Try google index.html or something.

After HTTP/1.1 you only have ^n, try using ^r^n. Not sure if it matters but it can't hurt to try.

"GET /%s HTTP/1.1^nHost: %s^r^n^r^n"

OFFBEAT 08-01-2011 21:49

Re: [Sockets]: Download a text for server usage
 
How stupid, it worked when i added www. before the address I figure it couldn't find the address without it.

Now a new small problem occured, I don't recieve the whole .txt file from the host, just a bit of it, like 1/5 or something.

EDIT: Fixed it with inserting a while loop. Now it works, thanks! ^^


All times are GMT -4. The time now is 03:27.

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