Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

UDP comm

Thread view: 
Andersen - 17 Nov 2005 08:49 GMT
How come I have to specify the size of the input buffer when receiving
UDP DatagramPackets? I basically want to receive as many bytes as the
sent datagram contains?

andersen
Gordon Beaton - 17 Nov 2005 08:32 GMT
> How come I have to specify the size of the input buffer when
> receiving UDP DatagramPackets? I basically want to receive as many
> bytes as the sent datagram contains?

You have to provide a buffer large enough for the largest expected
packet, and you have to tell the DatagramPacket how much of the buffer
it can fill. I suppose it could use buffer.length() itself, but there
may be cases when you don't want to fill the entire buffer.

After receiving a datagram, the length is updated to tell you how many
bytes were actually copied into the buffer.

It is extremely unfortunate that the same "length" is used for both
purposes, since it is far from obvious that you need to reset the
value before reusing the same DatagramPacket to receive additional
packets (otherwise you will receive successively smaller and smaller
packets, regardless of what is actually sent).

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Chris Uppal - 17 Nov 2005 09:47 GMT
> (otherwise you will receive successively smaller and smaller
> packets, regardless of what is actually sent).

Oooh, that's funny!

(Didn't quite ROFL, but did LOL)

   -- chris
Gordon Beaton - 17 Nov 2005 09:09 GMT
> Oooh, that's funny!
>
> (Didn't quite ROFL, but did LOL)

I remember swearing out loud when I discovered this cause of a UDP
communication problem.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Chris Uppal - 17 Nov 2005 11:36 GMT
[me:]
> > (Didn't quite ROFL, but did LOL)
>
> I remember swearing out loud when I discovered this cause of a UDP
> communication problem.

As with all coarse physical humour, it's not funny if /everyone/ laughs...

   -- chris
Roedy Green - 17 Nov 2005 12:27 GMT
>I remember swearing out loud when I discovered this cause of a UDP
>communication problem.

I documented it as a gotcha at http://mindprod.com/jgloss/udp.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 17 Nov 2005 10:35 GMT
On Thu, 17 Nov 2005 09:49:51 +0100, Andersen
<andersen_800@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>How come I have to specify the size of the input buffer when receiving
>UDP DatagramPackets? I basically want to receive as many bytes as the
>sent datagram contains?

the whole idea of a buffer is to have memory ready to go to collect
the incoming characters.  If your buffer is too small, then what is
supposed to happen to the characters coming in while you are
frantically allocating more space?

In a socket you have enough buffers pre-allocated so that you can
effectively send a "pause" message to the other end before you
overflow. Even if you overflow that is not the end of the world.  In
TCP/IP can request a retransmission.

With UDP if you don't get the packet the first time, it is gone. That
is why you need a buffer ready to take all the bits without a hitch.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.