Question:
how can i send a user defined object through datagram sockets by converting it into byte array in java?
shafali
2009-12-06 20:37:25 UTC
i want its coding. plz send me if any1 have it
Three answers:
single_minded
2009-12-06 21:24:45 UTC
Well,let me do some search for you.

You may know about this website, Almost got every example of java.Here you go,

http://www.java2s.com/Code/JavaAPI/java.net/DatagramSocket.htm



some other links;

http://www.java-samples.com/showtutorial.php?tutorialid=402

http://www.codase.com/search/call?owner=java.net.DatagramSocket

http://www.codeguru.com/java/tij/tij0166.shtml

http://www.javadocexamples.com/java/net/DatagramSocket/getLocalPort().html

http://www.roseindia.net/java/network/DatagramSocketExample.shtml

http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html

http://www.cs.nccu.edu.tw/~linw/javadoc/tutorial/networking/datagrams/clientServer.html

http://articles.techrepublic.com.com/5100-10878_11-5099670.html

http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
?
2009-12-06 23:39:48 UTC
Don't really feel like coding it for you, but here is some help. If you are any good at java it shouldn't take you more than an hour to code it yourself.



1. You might want to think twice about using a DatagramSocket. They use UDP (instead of TCP) as the underlying transport mechanism, and with UDP the reciever is not guaranteed to receive all of the packets. That's ok with stuff like video streaming where if you miss a packet, it only affects the quality a little, and even if the packet got re-sent it would show up too late to be useful. TCP on the other hand, which is what normal Sockets use, will guarantee that all data arrives at the destination. If you are sending java objects and missing a few bytes on the receiving end, then you are pretty much screwed.



2. As far as converting objects from and to byte arrays. Check out ObjectOutputStream & ByteArrayOutputStream (Obj to byte[]) and also ObjectInputStream & ByteArayInputStream (byte[] to object). I've used those combinations to store java objects to a MySQL Database before.



3. http://java.sun.com/javase/6/docs/api/ Check out the docs for the above classes plus the Socket and DatagramSocket for how to do the rest of it.





I've used XStream before: http://xstream.codehaus.org/ Not quite what you asked for, but very neat. It can automatically convert any java object to XML and from the XML to an object. Then you can just write the xml text over a TCP socket.
2016-10-29 04:47:11 UTC
i does not say switched over relatively. yet considered one of my terrific acquaintances and the guitarist for our band now did no longer comprehend lots in any respect approximately punk or pre-punk once I met him. He became right into a reasonably super steel head, nonetheless is to be honest, yet he had a tough time appreciating issues that weren't as complicated and quickly and technical as Slayer or Megadeth or Sepaltura, yet over the years for the reason that song is a vast element of the two our lives we uncovered one yet another to our very own musical tastes. and because then he's definitely discovered that some nicely placed chords or an straightforward yet catchy progression is nicely worth each and all of the means in the international. in spite of the undeniable fact that he nonetheless learn up on each and every thing and is ridiculously good at guitar of any type, the song he writes himself is purely approximately constantly a cool riff or progression and his CD series has a reasonably super punk element to it. And in tunr i've got taken a liking to a pair of his steel, in spite of the undeniable fact that I nonetheless won't be in a position to stand something with gruesome gruff vocals, like Sepultura or that form of component. I certainly have in spite of the undeniable fact that taken a wierd liking to the CD Reign In Blood by ability of Slayer, don't comprehend why.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...