> Why do you have to ping a host using Java?
>
> Why not just try to connect to it?
Because there's overhead in establishing a TCP or UDP connection if you just want to see if the host is there.
Because you don't necessarily know which services a given host may be offering, but you might be able to assume that it will respond to icmp echo request, and you just want to see if the host is minimally "alive".
Because you might want info about number of hops or roundtrip time or packet loss that's more easily obtained through ICMP than through a TCP connection.
> Why do you have to ping a host using Java?
>
> Why not just try to connect to it?
Of course, if you're doing to do
if (ping succeeds) {
open a socket
}
then yeah, that's pointless. Just open the socket.