[Gambas-user] Problem with READ on a UDP socket

Benoit Minisini gambas at ...1...
Tue Nov 4 15:35:21 CET 2008


On mardi 4 novembre 2008, Gareth Bult wrote:
> Hi,
>
> I think I reported this quite a while ago but it still seems to be a
> problem ... using READ to try to acquire less than the entire available
> buffer doesn't seem to work .. and if I read the entire buffer, there's no
> obvious way to break the packet down into it's constituent parts ...
>
> Anyone any ideas?
> (and anyone any idea where the other 24 bytes are going ??)
>
> PRIVATE $udp AS UdpSocket
>
> PUBLIC SUB _new()
>
> $udp = NEW UdpSocket AS "Socket"
> $udp.Bind(2000)
>
> END
>
> PUBLIC SUB Socket_Read()
>
> DIM cmd AS Byte
> DIM siz AS Long
>
> PRINT Lof($udp)
> READ #$udp, cmd, 1
> PRINT Lof($udp)
> 'READ #$udp, siz, 8 <= generates error if uncommented
>
> END
>
> $shell> echo "R00000011Hello World 123" |nc -u localhost 2000
>
> 25
> 0 <=== "should be 24!"

After having investigated, it seems that this is a property of the UDP 
sockets.

According to the manual page, when you read a message from a socket, "If a 
message is too long to fit in the supplied buffer, excess bytes may be 
discarded depending on the type of socket the message is received from".

You should not have this behaviour if you use a TCP socket.

But note that Gambas could be more user-friendly by maintaining an internal 
message buffer for UDP sockets, so that successive READs in the same event 
handler work as you expected. I will think about that.

Regards,


-- 
Benoit Minisini




More information about the User mailing list