The Java Socket class has two methods isClosed and isConnected to check whether the socket is closed or connected respectively. I want
This page gives a fairly good overview on socket states: http://diranieh.com/SOCKETS/SocketStates.htm and the difference between TCP and UDP sockets. Particularly:
Note that newer implementations of java.net.DatagramSocket support an extension to the TCP/IP network states: A DatagramSocket can also be in the state "connected", so that isConnected() does not necessarily return false even though a datagram socket is never "connected" at the network layer. In particular: "When a DatagramSocket is connected to a remote address, packets may only be sent to or received from that address. By default a datagram socket is not connected."
For more information, see the sources and the JavaDoc.