looking for example code for time server

Discuss any general programming issues here
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

looking for example code for time server

Post by Redlion »

Hi,

Iam looking for some example code for getting time from a time server using Hollywood, I have looked on the forum but have not found any.

Does some have an example of how to do this.

I have not done anything with networks and servers before, so I do not know where to start.

Cheers

Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: looking for example code for time server

Post by Redlion »

Hi All,

I have come up with a working example but it is not the recommended way.

Code: Select all

@DISPLAY {Hidden = true}

MUI.CreateGui([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application base="DidL">
<window title=" Internet time" id="window" width="640" notify="closerequest" >
    <vgroup background="pre_ShadowFill">
        <vgroup>
            <text id="timer"></text>
            <Button id="Quit" notify="pressed" >Quit</Button>
        </vgroup>
    </vgroup>
</window>
</application>
]])

Function p_MUIEvent(id) ; * Joint event handler for MUI buttons and menus ***********************************
    Switch id
             
        Case "Quit":
            End
    EndSwitch
EndFunction

Function p_EventFunc(msg)
    Switch msg.action
        Case "MUIRoyale":
            Switch msg.attribute
                Case "CloseRequest":
                     End
        
                Case "Pressed":
                    p_MUIEvent(msg.id)
        
            Endswitch

        Case "HideWindow":
            MUI.Set("app", "iconified", True)

        Case "ShowWindow":
            MUI.Set("app", "iconified", False)

    EndSwitch
EndFunction

; listen to these events!
InstallEventHandler({MUIRoyale = p_EventFunc, HideWindow = p_EventFunc, ShowWindow = p_EventFunc})

While Strlen(data$) =0
    OpenConnection(1, "time.nist.gov", 13)
        wait(25)
        data$  = ReceiveData(1,#RECEIVEALL)
    CloseConnection(1)
wend

mui.set("timer", "contents", data$)


; main loop!
Repeat
    WaitEvent
Forever
It is recommended to use the Network Time Protocol (NTP) but I can not get that to work.

I will keep looking and trying.


Cheers
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: looking for example code for time server

Post by Redlion »

@ Andreas
Hi Andreas, is there any chance that you could give me some example code using the

CreateUDPObject()
SendUDPData()
and
ReceiveUDPData()

I have tried and no combination seems to work, I do not know if it is the code or the server that is the problem. ( i am at work so I cant submit me code).

Thanks
Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: looking for example code for time server

Post by airsoftsoftwair »

Unfortunately, I don't know anything about the NTP protocol so I can't help with NTP specifics. Here is some example code from my test files that shows you how to use the UDP functions

udpserver.hws

Code: Select all

af = createudpobject(nil, 12345)
debugprint(getlocalport(af, #networkudp))

installeventhandler({onreceiveudpdata = function(msg)

	debugprint(msg.action, receiveudpdata(af))
	
	endfunction})

repeat
	waitevent
forever
udpsender.hws:

Code: Select all

createudpobject(1)

installeventhandler({onkeyup = function(msg)

	sendudpdata(1, msg.key, "127.0.0.1", 12345)
	
endfunction})

repeat
	waitevent
forever
First run udpserver.hws. Then run udpsender.hws. Whenever you press a key in udpsender.hws, it should then be received and printed by udpserver.hws.
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Re: looking for example code for time server

Post by sinisrus »

Hello,

I test the example but if I do

Code: Select all

sendudpdata (1, "Hello World", "127.0.0.1", 12345)
it only works with 1 single character
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: looking for example code for time server

Post by airsoftsoftwair »

Can't reproduce this. "Hello World" works fine here too. What platform are you on?
sinisrus
Posts: 347
Joined: Tue Apr 21, 2015 5:43 pm

Re: looking for example code for time server

Post by sinisrus »

WinUAE
plouf
Posts: 462
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: looking for example code for time server

Post by plouf »

i saw topic and willing to test a bit :)
tried the examples you have posted above as client-server tests

i found a problem
if test is as a single byte character arrives, but if the first character is NOT UTF compatible (raw data) compiler claims for utf error, or fails at all
i try with other tools (packetsender) to send data and as logn as a single character is above ByteChr(127) fails

Code: Select all

; Client example from above test files
createudpobject(1)

installeventhandler({onkeyup = function(msg)
	sendudpdata(1, bytechr(65), "127.0.0.1", 12345)  ; <- OK
	sendudpdata(1, bytechr(128), "127.0.0.1", 12345)  ; <-NOT  OK
endfunction})

repeat
	waitevent
forever
Christos
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: looking for example code for time server

Post by airsoftsoftwair »

sinisrus wrote: Wed Oct 14, 2020 11:30 pmWinUAE
True, I can confirm that. I'll see what's wrong there.
plouf wrote: Sat Oct 17, 2020 11:30 am i found a problem
if test is as a single byte character arrives, but if the first character is NOT UTF compatible (raw data) compiler claims for utf error, or fails at all
i try with other tools (packetsender) to send data and as logn as a single character is above ByteChr(127) fails
Which function call returns this UTF error? SendUDPData() or a different function?
plouf
Posts: 462
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: looking for example code for time server

Post by plouf »

receiveudp data(af)
Christos
Post Reply