[Gambas-user] gb3: sending variable of date datatype over UDP
Benoît Minisini
gambas at ...1...
Wed Jun 8 15:00:41 CEST 2011
> Any reason this code wouldn't work?
>
> ' Server-side:
>
> UDP_Write_56(pip[p], pport[p], tsid[p], Date)
>
> Public Sub UDP_Write_56(ip As String, port As Integer, id As Byte,
> serverdate As Date)
>
> ' Send "date update" transaction to specified IP address and port
> over UDP.
>
> ' Define target's IP address and port.
> udp.TargetHost = ip
> udp.TargetPort = port
>
> ' Send the transaction.
> udp.Begin
> Write #udp, id As Byte
> Write #udp, 56 As Byte
> Write #udp, serverdate As Date
> udp.Send
>
> End
>
> ' Client-side:
>
> ' tsdata = 8-byte date string from received UDP packet
> worlddate = Date@(Convert.Reverse(tsdata))
>
> Public Function Reverse(data As String) As String
>
> ' Reverse the order of the passed bytes.
>
> ' General declarations.
> Dim counter As Byte ' Generic counter.
> Dim length As Byte ' Length of the passed string.
> Dim reversed As String ' The reversed string.
>
> ' Reverse the string
> length = Len(data)
> For counter = length To 1 Step -1
> reversed = reversed & Mid$(data, counter, 1)
> Next
>
> ' Return the reversed value.
> Return reversed
>
> End
>
> If I send and receive it as a float it works, but doesn't give me the
> time (date only). If I send/receive it as a date it gives me a totally
> wrong value, such as "10883/26232/-22744 285:08:28". Combinations of the
> two give me different incorrect values.
>
> This technique of reversing bytes received as UDP packets into different
> datatypes has worked for everything but the date datatype. Any ideas why
> this is? I need the clients to know what time it is on the server so
> they can render day/night cycles appropriately.
Date are sent as two 32-bits integers. So you must not reverse the all string,
but each four bytes part indepently.
Regards,
--
Benoît Minisini
More information about the User
mailing list