Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ping code

 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a java code to ping any IP in an infinite loop. Anybody up??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Jeff. That was quite a heavy dose. What are these UDP , ICMP by the way? I know only about TCP/IP.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The TCP/IP stack contains a lot of protocols other than TCP and IP: TCP/IP
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for the “beginning” forum. Moving.
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Thanks all for the clarifications
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic