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 / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

Pb. receiving UDP datagrams

Thread view: 
nospam.eric@gmail.com - 03 Oct 2006 12:09 GMT
Hi,

I receive a continuous flow of UDP datagrams on my PC that I would like
to process in a Java program. I wrote the following piece of code but I
can't get any datagram (the .receive() function blocks forever). I
checked with a network sniffer - Ethereal/WireShark - and I could
verify the presence of a continuous UDP flow with valid UDP datagram
headers (destination port: 8081; checksum = 0 (none)).

Could someone help me finding out what I'm doint wrong?

Eric

PS: The piece of code:

public final class UDPClient {
   private static final Logger logger = Logger.getRootLogger();
   static byte[] buffer = new byte[65507];

   public static void main(String[] args) {
       try {
           DatagramSocket ds;
           ds = new DatagramSocket(8081);
           DatagramPacket dpi;
           dpi = new DatagramPacket(new byte[512], 512);
           while(true) {
               ds.receive(dpi);
               logger.info("UDP datagram received from " +
dpi.getAddress() + ":" + dpi.getPort());
           }
       } // end try
       catch (Exception e) {
           System.err.println(e);
       } // end catch
   }
}
Chris Uppal - 03 Oct 2006 12:28 GMT
>             DatagramSocket ds;
>             ds = new DatagramSocket(8081);
>             DatagramPacket dpi;
>             dpi = new DatagramPacket(new byte[512], 512);
>             while(true) {
>                 ds.receive(dpi);

Comparing this with some working code of my own, the only difference I can see
is that I set the size of the DatagramPacket explicitly before each receive().
My code reads:

   DatagramSocket socket = new DatagramSocket(PORT);
   DatagramPacket packet = new DatagramPacket(
                                                new byte[BUFFER_SIZE],
                                                BUFFER_SIZE);
   for (;;)
   {
       packet.setLength(BUFFER_SIZE);
       socket.receive(packet);
       ...

The Java network stuff treats the length of the packet as separate from the
length of the buffer which is used to send/receive it.

   -- chris
Gordon Beaton - 03 Oct 2006 13:55 GMT
> The Java network stuff treats the length of the packet as separate
> from the length of the buffer which is used to send/receive it.

Unfortunately the length associated with the DatagramPacket is used
for two purposes: to tell receive() how much you want to receive, and
to find out how much was actually received.

So if you attempt to reuse a DatagramPacket but forget to call
setLength() before receive(), you will find that your receives can
only get shorter as a result, never longer, until you reach 0.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



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.