Raised This Month: $51 Target: $400
 12% 

Socket


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xablau
Member
Join Date: Dec 2014
Old 01-16-2015 , 16:46   Socket
Reply With Quote #1

How to get data from page an independent of your size?


In:

PHP Code:
    new g_Socketg_Data[2000], sendbuffer[512]
    
g_Socket socket_open("127.0.0.1"80SOCKET_TCPerror)
    
format(sendbuffer1024"GET /data.php HTTP/1.1^nHost: 127.0.0.1^n^n")
    
socket_send(g_Socketsendbuffer1024)
    
    if (
socket_change(g_Socket))
    {
        
socket_recv(g_Socketg_Data999)
        
server_print("Data: %s"g_Data)
    }
    
socket_close(g_Socket
Is not picking up all the page content, why?
Xablau is offline
Xablau
Member
Join Date: Dec 2014
Old 01-17-2015 , 17:08   Re: Socket
Reply With Quote #2

Socket has a limit? I can not solve this problem ...
Xablau is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 01-17-2015 , 18:23   Re: Socket
Reply With Quote #3

Code:
socket_recv(g_Socket, g_Data, 999)
there's your limit, you read 999 bytes of data (and for some bizarre reason you declare the g_Data array with 2000 cells)
jimaway is offline
Xablau
Member
Join Date: Dec 2014
Old 01-17-2015 , 19:29   Re: Socket
Reply With Quote #4

My data.php page:

PHP Code:
<?php
header
('Content-Type: application/json');
?>
{"hats":[{"id":"1","name":"Meu Hats","arquivo":"hat.mdl"},{"id":"2","name":"Meu Hat 2","arquivo":"hat2.mdl"}],"hats":"2"}
server_print("Data: %s", g_Data) return only:

Code:
Json: HTTP/1.1 200 OK
Date: Sun, 18 Jan 2015 00:27:14 GMT
Server: Apache/2.4.10 <Win32> OpenSSL/1.0.1i PHP/5.6.3
X-Powered-By: PHP/5.6.3
Content-Length: 122
Content-Type: application/json

{"hats":[{"id":"1","name":"Meu Hats","arquivo":"hat.mdl"},
Why?

Last edited by Xablau; 01-17-2015 at 19:30.
Xablau is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-17-2015 , 22:08   Re: Socket
Reply With Quote #5

It's possible that server_print() will only show up to X characters per print, but I'm not sure on that one.

There are some instances where you will not receive all data in a single socket_recv() call. I quickly put this together to show you how to get an entire file. It will write all received data to TESTFILE.txt in your mod directory.
PHP Code:
#include <amxmodx>
#include <sockets>

new g_Socket;
const 
TASKID 5213;

public 
plugin_init() 
{
    
register_concmd"test" "test" );
}

public 
test()
{
    new 
sendbuffer[512] , error

    g_Socket 
socket_open"www.google.com" 80 SOCKET_TCP error );
    
    if ( 
g_Socket && !error )
    {
        
formatexsendbuffer charsmaxsendbuffer ) , "GET /index.html HTTP/1.1^nHost: www.google.com^n^n" );
        
socket_sendg_Socketsendbuffer sizeofsendbuffer ) );
        
        
//Create a task to check for data every second (you would want a faster interval in real-world situations)
        
set_task1.0 "CheckForData" TASKID , .flags="b" );

        
//Create a task to close the socket and stop checking for data after 60 seconds. There are cleaner ways, this 
        //was just a fast method for demonstration.
        
set_task60.0 "CloseSocket" );
    }
}

public 
CheckForData()
{
    static 
g_Data[2000];
    new 
iRecv;
    
    
server_print("Checking for data")
    
    if ( 
socket_changeg_Socket ) )
    {
        
iRecv socket_recv(g_Socketg_Datacharsmaxg_Data ) )
        
server_print("Recv=%d Data: %s"iRecv g_Data)
        
write_file"TESTFILE.txt" g_Data );
    }
}

public 
CloseSocket()
{
    
socket_closeg_Socket );
    
remove_taskTASKID );

__________________

Last edited by Bugsy; 01-18-2015 at 12:14.
Bugsy 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 13:04.


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