Raised This Month: $ Target: $400
 0% 

Sockets (Getting Time)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drak
Veteran Member
Join Date: Jul 2005
Old 04-04-2009 , 20:32   Sockets (Getting Time)
Reply With Quote #1

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"?
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
hlstriker
Green Gaben
Join Date: Mar 2006
Location: OH-IO!
Old 04-05-2009 , 22:26   Re: Sockets (Getting Time)
Reply With Quote #2

Good to see you around again, Drak

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

Last edited by hlstriker; 04-05-2009 at 22:34.
hlstriker is offline
Drak
Veteran Member
Join Date: Jul 2005
Old 04-08-2009 , 23:05   Re: Sockets (Getting Time)
Reply With Quote #3

Yesss. That worked, yeah. The data is sent in bursts so now I can string attack it and get the time. Thanks
__________________
Oh yeah
Drak is offline
Send a message via MSN to Drak
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-09-2009 , 00:15   Re: Sockets (Getting Time)
Reply With Quote #4

Quote:
Originally Posted by hlstriker View Post
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.
__________________

Last edited by Bugsy; 04-09-2009 at 01:05.
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 02:23.


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