AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sockets (Getting Time) (https://forums.alliedmods.net/showthread.php?t=89320)

Drak 04-04-2009 20:32

Sockets (Getting Time)
 
I have:
Code:
#include <amxmodx> #include <amxmisc> #include <sockets> #define HOST "192.168.1.133" #define FILE "/amxx.php" //http://ozforce.my-place.us/amxx.php #define SCRIPT_NAME "/myplugin/parser.php" #define REMOTE_HOST "myserver.com" //port d.80 new g_TimeSocket new g_GetSocket public plugin_init()     register_clcmd("say !sock","CmdSock"); public CmdSock(id) {     new Error     g_TimeSocket = socket_open("www.worldtimeserver.com",80,SOCKET_TCP,Error);         if(Error != 0)     {         client_print(id,print_chat,"[TEST] Unable to Open Socket.");         return PLUGIN_HANDLED     }         client_print(id,print_chat,"[TEST] Socket Opened.");     set_task(1.5,"SockSend");         return PLUGIN_HANDLED } public SockSend() {     new Buffer[128]     format(Buffer,127,"GET %s HTTP/1.1^nHost: %s^n^n","current_time_in_MV.aspx","www.worldtimeserver.com");         socket_send(g_TimeSocket,Buffer,127);     set_task(1.5,"SockGetData");         client_print(0,print_chat,"[TEST] Socket Sent."); } public SockGetData() {     new Buffer[1024]         if(!socket_change(g_TimeSocket,100))     {         client_print(0,print_chat,"[TEST] No Data");         return set_task(1.0,"SockGetData");     }         socket_recv(g_TimeSocket,Buffer,1023);     socket_close(g_TimeSocket);         client_print(0,print_console,"[TEST] I got: %s",Buffer);     client_print(0,print_chat,"[TEST] Socket Recieved.");     return 1 }
And I get a "400 Bad Request" Error. So I know everything is working. But how exactly do I work with this? I know that "http://www.worldtimeserver.com/current_time_in_MV.aspx" returns the time. I figured it would return the HTML Source of the page. Any ideas how I can get the data (time) from "http://www.worldtimeserver.com/current_time_in_MV.aspx"?

hlstriker 04-05-2009 22:26

Re: Sockets (Getting Time)
 
Good to see you around again, Drak :D

1) First off in your request you should use '\r\n' instead of '\n' (not sure if this matters for Linux, I assume it does?).

Example:
Code:

GET /current_time_in_MV.aspx HTTP/1.1\r\n
Host: www.worldtimeserver.com\r\n

2) Now, you are also missing the connection parameter.

Example:
Code:

Connection: Close\r\n\
To tell the connection we are finished just send another new line '\r\n'.

So the complete example looks like this:
Code:

GET /current_time_in_MV.aspx HTTP/1.1\r\n
Host: www.worldtimeserver.com\r\n
Connection: Close\r\n\r\n

3) If you plug this into your code it should return the pages source. Now you have a problem because you need a buffer that can read ALL the html. I've never retrieved data with AMXX sockets before so I'm not sure if you can read the data in bursts or not.

4) Now you need to select the time out of all the html. It will probably be best to use regex for this.

Hopefully this can help you some :)

Drak 04-08-2009 23:05

Re: Sockets (Getting Time)
 
Yesss. That worked, yeah. The data is sent in bursts so now I can string attack it and get the time. Thanks :D

Bugsy 04-09-2009 00:15

Re: Sockets (Getting Time)
 
Quote:

Originally Posted by hlstriker (Post 798498)
Now you have a problem because you need a buffer that can read ALL the html. I've never retrieved data with AMXX sockets before so I'm not sure if you can read the data in bursts or not.

You could use a modestly sized buffer [512-1024 bytes] and re-use the space as new data comes in. Just search for a character in the buffer that you know is not a part of the search string you are looking for and chop it off there.


All times are GMT -4. The time now is 02:23.

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