GLBasic forum

Main forum => GLBasic - en => Topic started by: FutureCow on 2010-Mar-08

Title: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-08
Anyone have any ideas?
I'm trying to query an NTP server with the following (see page http://www.kloth.net/software/timesrv1.php for example code)

Code (glbasic) Select
LOCAL server$, ok%, sock%, rv%
ok%=SOCK_INIT()
sock% = SOCK_TCPCONNECT("0.pool.ntp.org", 37)
rv = SOCK_TCPSEND (sock, "\n")
LOCAL Done = 0

WHILE Done = 0
rv = SOCK_RECV(sock%, msg$, 1024)
IF rv = -2
SLEEP 1
ENDIF

IF KEY(1) = 1
Done = 1
ENDIF
WEND


The code gets as far as the SOCK_TCPCONNECT and locks up. Is there a problem doing a socket connection to a server you didn't start?
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-08
The site is a pool of IP addresses, so it changes the host it actually points to every hour.

Regardless, I wouldn't expect that a SOCK_TCPCONNECT to a non-listening host should lock up.
Title: Re: SOCK_TCPCONNECT lock up
Post by: Kitty Hello on 2010-Mar-09
For me it times out after 5 seconds, returning -1 as expected.
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-09
Is there any sort of debug that I can do?
Title: Re: SOCK_TCPCONNECT lock up
Post by: MrTAToad on 2010-Mar-09
It might be worth changing the code slightly :

Code (glbasic) Select
LOCAL server$, ok%, sock%, rv%
ok%=SOCK_INIT()
sock% = SOCK_TCPCONNECT("0.pool.ntp.org", 37)
rv = SOCK_TCPSEND (sock, "\n")
LOCAL Done = 0

WHILE Done = 0
rv = SOCK_RECV(sock%, msg$, 1024)
IF rv = -2
SLEEP 1
ENDIF

IF KEY(1) = 1
Done = 1
ENDIF

DEBUG "Here\n"
SLEEP 1000
WEND


And run in debug mode.

On mine there was a 23 second wait until the code after SOCK_RECV started executing (during which the program is classed as being no responsive).  However, once that is passed, it does respond to key presses (albeit you have to hold down ESC)...

RV returns -1 (which is an error)
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-10
Okay, I was being impatient, it takes ~ 15 seconds to die. I was trying to killing it at about the 10 second mark, and the program reports "not responding" until the SOCK_TCPCONNECT dies.
The port I'm trying to reach is blocked by the firewall here, so I'm not sure why it doesn't die immediately. Regardless, it eventually comes back with a
Quoteast error = 10038 An operation was attempted on something that is not a socket.

Is there any C++ hack anyone knows of to shorten that timeout?
Title: Re: SOCK_TCPCONNECT lock up
Post by: Moru on 2010-Mar-10
Do you have any information on how to use the NTP protocol? The NTP server usually only returns a packet with timestamps in 64 bit format (seconds since 1968 or whatever) so you need to figure out what timestamp your friend has a birthday. I don't think that function is included in GLBasic but I'm sure someone can write some inline code for it :-)

I also thought NTP was over UDP, not TCP?

Use some simple PHP script instead, way easier than NTP or even SNTP :-)
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-10
The Internet Time Service (Port 37) is both TCP and UDP. It's actually seconds since 1/1/1900 (as opposed to the typically used Unix epoch - 1/1/1970).
It's easy to do a quick bit of maths and work out a number of seconds that correspond to the date I need (or use an internet site  =D) so it should be a very easy process.
NTP (port 123) gets rather complicated, but the internet time service is easy to use provided I can get GLBasic to send a "\n" to the time server which is what I was trying to do when I had the problem. I don't know PHP , and though I could use perl to query NTP I'd need to install perl on the machine that will have the birthday message and that just gets messy. It's easier just to get GLBasic to open an internet connection, read a number of seconds, and see if that number of seconds is greater than midnight on the day I'm looking for.
Title: Re: SOCK_TCPCONNECT lock up
Post by: Kitty Hello on 2010-Mar-10
I'll add a command to set the timeout values. The default is 75 secs or so, which is probably too long these days.
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-10
Awesome!
Title: Re: SOCK_TCPCONNECT lock up
Post by: Moru on 2010-Mar-10
Ocean: Hush, he wants the complicated version :-)
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-10
*laugh*
Funnily enough, the "harder" version is probably easier as I still have to convert from GMT. Doing one comparison to a number of seconds is (at face value) likely to be a lot easier than substringing the "easier" answer, then doing calculations from GMT to the Australian timezone.
Having said that though, from a quick test, telnetting to port 37 seemed to return binary data (I'm just about to investigate it) so it may not be the easiest solution after all.

And all this to ensure they don't change the system clock (which they probably wouldn't try anyway as it's for a birthday present.  :whistle: )
Title: Re: SOCK_TCPCONNECT lock up
Post by: Moru on 2010-Mar-10
You can do the whole conversion thing to the right timezone all in PHP with two commands, let me know if you want help with it.
Title: Re: SOCK_TCPCONNECT lock up
Post by: FutureCow on 2010-Mar-10
Thanks Moru, I'll let you know if I decide to go with php.
Title: Re: SOCK_TCPCONNECT lock up
Post by: Moru on 2010-Mar-10
Actually I had a few minutes so I corrected my date.php to Australia/Sydney, if you want some other timezone, choose one here: http://se.php.net/manual/en/timezones.australia.php

I included the time and timezone so you can verify that it works correctly.
http://gamecorner.110mb.com/date.php

Code if you don't trust my free server and want to host it yourself somewhere :-)
Code (glbasic) Select
<?php 

date_default_timezone_set
('Australia/Sydney');

echo 
date('Y-m-d');
echo 
"<br>\n";
echo 
date('c');
echo 
"<br>\n";
echo 
date_default_timezone_get();