aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Ping code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Ping code" Watch "Ping code" New topic
Author

Ping code

Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
I need a java code to ping any IP in an infinite loop. Anybody up??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Two things:

1) This site is NotACodeMill(⇐click). People here are happy to help (That's why we're here after all!), but you have to ShowSomeEffort(⇐click).

2) You'll have to define what you mean by "ping." If you're talking about the same thing that the command-line ping program does, that's not possible in pure Java, since it operates at a lower level in the network stack than the core APIs give us access to.
Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
Well there is no serious code for this for which you expect me to try. I just don't know the class required for it (and the methods to be used).


Jeff Verdegan wrote:

2) You'll have to define what you mean by "ping." If you're talking about the same thing that the command-line ping program does, that's not possible in pure Java, since it operates at a lower level in the network stack than the core APIs give us access to.


Then I would like to know the way to generate packets of my own for the destination.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mukul Anand wrote:Well there is no serious code for this for which you expect me to try. I just don't know the class required for it (and the methods to be used).


Have you tried to SearchFirst(⇐click)? Have you tried studying networking in Java at all? Again, this site is not for handing out code.


Jeff Verdegan wrote:

2) You'll have to define what you mean by "ping." If you're talking about the same thing that the command-line ping program does, that's not possible in pure Java, since it operates at a lower level in the network stack than the core APIs give us access to.


Then I would like to know the way to generate packets of my own for the destination.


Your requirements are still very vague. However, if you want to "generate packets," then look at java.net.DatagramPacket and java.net.DatagramSocket.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

I don't think using DatagramPacket or DatagramSocket is going to work. As far as I know, you can use those to send data via the UDP protocol, which is one of the other protocols used on the Internet besides TCP.

Ping uses another protocol; ICMP, which is different from TCP or UDP.

As already said, Java's standard library doesn't have an API for working with IP network protocols on such a low level. There's not a class available in the standard library that you can easily use to do what ping does. If you really want to do this, it's probably not going to be possible in pure Java. You'll need to write some operating system specific native code (for example in C or C++) that you'll call from your Java program.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesper de Jong wrote:I don't think using DatagramPacket or DatagramSocket is going to work. As far as I know, you can use those to send data via the UDP protocol, which is one of the other protocols used on the Internet besides TCP.


Right. And I certainly didn't mean to imply that datagrams could implement a "real" ping. Only that, depending on his requirements, they might be a candidate for a ping-like "are you there?" operation. @OP: Sorry if I caused any confusion.
Steven Schwab
Greenhorn

Joined: May 25, 2010
Posts: 9
Your closest match to the ping command is probably http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int), which performs an ICMP ECHO request, if it can.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Steven Schwab wrote:Your closest match to the ping command is probably http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int), which performs an ICMP ECHO request, if it can.


Unfortunately, that "if it can" qualifier puts a major kink in it. Although I haven't tried it recently, in the past, every time I tried it, on both Windows and various Linuces, it fell back to just the echo service on port 7, which I don't think hosts typically have running.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

OK. So having an underlying virtual machine takes away the privilege one has when working with languages such as C/C++ etc to work at a lower level. Some trade off this.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:OK. So having an underlying virtual machine takes away the privilege one has when working with languages such as C/C++ etc to work at a lower level. Some trade off this.


Not really.

It's not that the presence of a VM inherently removes that ability. It was a deliberate design decision by the creators of the language and API to leave it out. And for Java's target audience, it's not that much of a trade-off. It's not that often, in the context for which Java was intended, that we need ICMP and such lower-level networking constructs. The TCP and UDP tools provided cover the large majority of use cases.

Even if Java provided an ICMP library, or even if you were to write your own ping in C, it wouldn't be as simple as that. Typically, root/admin access is required for the raw socket operations necessary for an ICMP ping. In Linux, ping is a setuid program. That means that when the program is run, the effective user id becomes root. Without that, general users wouldn't have the raw socket permissions needed. On Windows, at least in Windows 7, you can't run ping from a console unless you do Run As Administrator to start that console.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

OK Jeff. That was quite a heavy dose. What are these UDP , ICMP by the way? I know only about TCP/IP.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
The TCP/IP stack contains a lot of protocols other than TCP and IP: TCP/IP


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38888
    
  23
Too difficult for the “beginning” forum. Moving.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Mansukhdeep Thind wrote:OK Jeff. That was quite a heavy dose. What are these UDP , ICMP by the way? I know only about TCP/IP.


Ulf Dittmer wrote:The TCP/IP stack contains a lot of protocols other than TCP and IP: TCP/IP



Well, the good news is, unless you plan to specialize in networking, or do stuff lower than application programming (such as systems level programming), the chances of needing to know most of these networking protocols are slim.

On the other hand, the UDP protocol are sometimes encountered by application programmers, so it would be a good idea to learn it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
Okay. Thanks all for the clarifications
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ping code