This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
How do I listen for ping commands. Another machine will try to ping mine and I must detect it in my application and display a message to the user. Is there any predefined API that I can use? Or how do i go on writing the code on my own?
If you're talking about the actual ICMP protocol used by the ping command, you can't do it in pure Java, as that is a network layer protocol and Java's built-in networking API only goes down to the transport layer.
If you're using "ping" in a more generic sense, such as "heartbeat," that is, "the other host has to send some small packet and I have to respond with a small packet," then you can implement that however you want. You could listen on a particular socket for a TCP connection, or you could just listen for UDP packets. Other than that, any details about how you implement it would depend on your specific requirements and constraints.