Raised This Month: $ Target: $400
 0% 

[Sockets]: Download a text for server usage


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OFFBEAT
Junior Member
Join Date: Feb 2011
Location: Sweden
Old 08-01-2011 , 14:38   [Sockets]: Download a text for server usage
Reply With Quote #1

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;


Last edited by OFFBEAT; 08-01-2011 at 20:51.
OFFBEAT is offline
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 08-01-2011 , 15:04   Re: [Sockets]: Download a text for server usage
Reply With Quote #2

socket_open( "kz-scandinavia.com", 80, SOCKET_TCP, error );
AoD90 is offline
Send a message via MSN to AoD90
OFFBEAT
Junior Member
Join Date: Feb 2011
Location: Sweden
Old 08-01-2011 , 16:06   Re: [Sockets]: Download a text for server usage
Reply With Quote #3

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.
OFFBEAT is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 08-01-2011 , 20:15   Re: [Sockets]: Download a text for server usage
Reply With Quote #4

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.
__________________
xPaw is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-01-2011 , 20:37   Re: [Sockets]: Download a text for server usage
Reply With Quote #5

Take a look at this

http://forums.alliedmods.net/showpos...85&postcount=5
__________________
Bugsy is offline
OFFBEAT
Junior Member
Join Date: Feb 2011
Location: Sweden
Old 08-01-2011 , 20:48   Re: [Sockets]: Download a text for server usage
Reply With Quote #6

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

Last edited by OFFBEAT; 08-01-2011 at 21:08.
OFFBEAT is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-01-2011 , 21:15   Re: [Sockets]: Download a text for server usage
Reply With Quote #7

Show your HTTP request packet
__________________
Bugsy is offline
OFFBEAT
Junior Member
Join Date: Feb 2011
Location: Sweden
Old 08-01-2011 , 21:18   Re: [Sockets]: Download a text for server usage
Reply With Quote #8

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)
OFFBEAT is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-01-2011 , 21:35   Re: [Sockets]: Download a text for server usage
Reply With Quote #9

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"
__________________
Bugsy is offline
OFFBEAT
Junior Member
Join Date: Feb 2011
Location: Sweden
Old 08-01-2011 , 21:49   Re: [Sockets]: Download a text for server usage
Reply With Quote #10

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! ^^

Last edited by OFFBEAT; 08-01-2011 at 21:53.
OFFBEAT is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:01.


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