Author Topic: [JAVA] Issue replying to Datagrams  (Read 3965 times)

0 Members and 1 Guest are viewing this topic.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
[JAVA] Issue replying to Datagrams
« on: June 22, 2010, 06:49:10 pm »
Some machines on my network are sending datagrams to me and I am trying to reply to them, but I am not sure how to code a datagram listener. The code below works (well, it receives the datagrams), but when I try to get the InetAddress it originated from, it is either null or my own InetAddress (I suppose it varies by what I try, but I always get one of those two results). How can I get the correct InetAddress to reply with another datagram?

Code: [Select]
setSocket(new DatagramSocket(1234)); // Just a setter for a DatagramSocket instance.

...

byte[] raw = new byte[256];
DatagramPacket datagram = new DatagramPacket(raw, raw.length);
getSocket() // Just a getter for a DatagramSocket instance.
           .receive(datagram);

...

datagram.getInetAddress(); // Either null or my InetAddress

Thanks :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!