• Post Reply Bookmark Topic Watch Topic
  • New Topic

NIO DatagramChannels

 
Chris Holcombe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get my DatagramChannel to receive input after it sends a packet out on the network. I want to be able to send a receive on the same socket (non-blocking). It's not throwing any exceptions, it's just not getting data. My infinite while loop is in there just for testing the network. The whole problem is that the receive method just doesn't fill my buffer. Any reason why?
 
Ron Hitchens
Author
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you expecting to receive the same packet you just sent? Or is someone else sending you packets? You're using a broadcast address as the destination, perhaps you don't have privilege to broadcast.
Rather than spinning in a loop, I suggest you setup a Selector which will wait for something to appear on the socket. In any case, you should check the return value from receive(). It will be non-zero when a datagram has been received.
I have some code that uses DatagramSockets among the example code for my book. You can download it from
here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!