What is the difference between DatagramSocket and Socket?

I have two separate programs to do with IP communications. One uses DatagramPacket and DatagramSocket whilst the other uses ServerSocket and Socket. What is the difference and which is more efficient to use?
[214 byte] By [markyoung1984a] at [2007-10-2 21:07:45]
# 1

DatagramSocket is for sending UDP/IP datagrams. Datagrams are individual packets. They are unreliable: they may be dropped, duplicated, and delivered out of order. Use these when you need packets and you don't mind losing part of the data. (Or if you are prepared to build a reliability system on top of UDP, but that's generally silly because TCP already does that better than you'll be able to do it.)

Socket is a stream based communications device and uses TCP/IP. There are no message boundaries, TCP delivers a stream of bytes. TCP is reliable: if data are lost on the way, TCP automatically re-sends (up to a point, it will eventually give up and return an error).

"Which is more efficient" is like asking "which is more efficient: a halibut or the color mauve". They are different things.

sjasjaa at 2007-7-13 23:53:27 > top of Java-index,Java Essentials,New To Java...