Page 1 of 2

looking for example code for time server

Posted: Wed Jan 11, 2017 12:58 am
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

Re: looking for example code for time server

Posted: Thu Jan 12, 2017 8:21 am
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

Re: looking for example code for time server

Posted: Mon Jan 16, 2017 11:31 am
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

Re: looking for example code for time server

Posted: Tue Jan 17, 2017 7:56 pm
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.

Re: looking for example code for time server

Posted: Wed Oct 14, 2020 3:51 pm
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

Re: looking for example code for time server

Posted: Wed Oct 14, 2020 10:45 pm
by airsoftsoftwair
Can't reproduce this. "Hello World" works fine here too. What platform are you on?

Re: looking for example code for time server

Posted: Wed Oct 14, 2020 11:30 pm
by sinisrus
WinUAE

Re: looking for example code for time server

Posted: Sat Oct 17, 2020 11:30 am
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

Re: looking for example code for time server

Posted: Sun Oct 18, 2020 11:13 pm
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?

Re: looking for example code for time server

Posted: Mon Oct 19, 2020 6:07 am
by plouf
receiveudp data(af)