[Gambas-user] gb3: sending variable of date datatype over UDP

Kevin Fishburne kevinfishburne at ...1887...
Thu Jun 9 03:58:36 CEST 2011


On 06/08/2011 09:00 AM, Benoît Minisini wrote:
>> 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.

Got it. Funny datatype, no? What's interesting however is that this code:

worlddate = Date@(Convert.Reverse(Mid$(tsdata, 1, 4)) & 
Convert.Reverse(Mid$(tsdata, 5, 4)))

gives me the correct date but not the correct time.

The date appears as "06/08/11" but the time appears as "05:00:00" when I 
use the code ?Time(worlddate) in the console. Maybe there's a different 
way to extract the time from a date datatype, or maybe I'm doing 
something wrong in reverse-engineering the datatype.

The good news is that I think I just realized that I don't need the date 
sent to the client, just the time for day/night cycles. The client can 
use that time value plus Timer to determine the current server time.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list