looking for example code for time server

Discuss any general programming issues here

looking for example code for time server

Postby Redlion » Wed Jan 11, 2017 1:58 am

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: 62
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: looking for example code for time server

Postby Redlion » Thu Jan 12, 2017 9:21 am

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: 62
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: looking for example code for time server

Postby Redlion » Mon Jan 16, 2017 12:31 pm

@ 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
Redlion
 
Posts: 62
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: looking for example code for time server

Postby airsoftsoftwair » Tue Jan 17, 2017 8:56 pm

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.
User avatar
airsoftsoftwair
 
Posts: 2202
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany


Return to General programming

Who is online

Users browsing this forum: No registered users and 2 guests