0

I'm implementing this example as I want to broadcast to multiple clients. While trying to use the socket to send another object(which has been serialised), I'm getting an error which says that socket(in the example) can't send the object via it. Can't a datagram socket send/receive objects also?

P R
  • 1,293
  • 7
  • 28
  • 58
  • Show us your code, and tell us the exact error message you get. – JB Nizet Oct 02 '11 at 17:11
  • datagram is not the best for sending objects since you aren't guaranteed that all the packets will arrive. – Hovercraft Full Of Eels Oct 02 '11 at 17:18
  • 1
    Some useful links: [Java Tip 40: Object transport via datagram packets](http://www.javaworld.com/javaworld/javatips/jw-javatip40.html) and [Send and receive serialize object on UDP in java](http://stackoverflow.com/questions/3997459/send-and-receive-serialize-object-on-udp-in-java) – Hovercraft Full Of Eels Oct 02 '11 at 17:24

2 Answers2

4

Not in general, no. Datagram packets are generally relatively small - you could try serializing your object to a ByteArrayOutputStream wrapped in an ObjectOutputStream, and then try to send the byte array afterwards - but you may well find it gets too big very quickly.

Using a more efficient serialization format such as Protocol Buffers will probably allow you to get more information in a single packet, but typically you'd want to serialize to a stream instead of to a single packet... and as soon as you start trying to put a stream-based protocol over a datagram-based protocol - well, you end up with TCP reasonably quickly, as soon as it has to be reliable.

If you can give us more details of what you're trying to do (including reliability constraints - how serious is it if a packet is lost?), we may be able to help you more.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Your best bet is to either use TCP or another library such as jGroups

JGroups is a toolkit for reliable multicast communication.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79