AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Socket (https://forums.alliedmods.net/showthread.php?t=255570)

Xablau 01-16-2015 16:46

Socket
 
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 01-17-2015 17:08

Re: Socket
 
Socket has a limit? I can not solve this problem ...

jimaway 01-17-2015 18:23

Re: Socket
 
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)

Xablau 01-17-2015 19:29

Re: Socket
 
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?

Bugsy 01-17-2015 22:08

Re: Socket
 
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 );




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

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