1 2 3 Previous Next 35 Replies Latest reply on Aug 8, 2006 3:00 PM by 807592 Go to original post
      • 15. Re: How to write a ping program in java
        3004
        Something on this site that was less than advertised?!? Oh the shock.
        • 16. Re: How to write a ping program in java
          796365
          I just ran robtaft version and it gave
          16
          0
          0
          0

          Obviously my Java is better! :)
          • 17. Re: How to write a ping program in java
            791266
            I checked the bug database but I can't find a single bug that is related to isReachable and high pings.
            • 18. Re: How to write a ping program in java
              796365
              I bet it's this one, put in the 06 update:
              http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5092063
              • 19. Re: How to write a ping program in java
                796447
                I bet it's this one, put in the 06 update:
                http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=509
                2063
                If it's truly fixed in 1.5.0_06, then it sure would be swell if Sun would actually deliver that version when you pick it from their download page (remember, it gave me 1.5.0_04 even though the link clearly said otherwise).
                • 20. Re: How to write a ping program in java
                  791266
                  I bet it's this one, put in the 06 update:
                  http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=509
                  2063
                  True, that looks like it.
                  • 21. Re: How to write a ping program in java
                    456795
                    Mine also took around a second. I think I might know why it takes so long. I noticed that java tried to communicate on the echo (7) port when trying to run that program. I bet the implementation is trying to use tcp and connection to port 7 and when that fails it then tries ICMP. I am still not sure it is even trying to do ICMP since my ICMPv4 Statistics didn't update as it does with the commandline program ping.

                    @ChuckBing. Do you have port 7 (echo) open on your box?

                    In the meantime I am going to see if I can find a windows program that will do tcp echo and see if the times are different.
                    • 22. Re: How to write a ping program in java
                      807590
                      It seems to take a second for Windows to return "connection refused" when trying to connect to a port with no listening program. I'm sure there is a perfectly good reason for that...

                      This is repeatable from C as well as Java. Demo programs below.

                      isReachable() tries to connect to port 7 (echo). It doesn't use ICMP ECHO (= ping) on Windows because, according to comments in the source, "Windows implementation of ICMP & RAW sockets is too unreliable for now."
                      import java.net.*;
                      import java.util.*;
                      import java.io.*;
                      
                      public class ConnectEcho
                      {
                          public static void main(String args[])
                           throws Exception
                          {
                           for (int n = 0; n < 5; n++) {
                               long start = System.currentTimeMillis();
                               try {
                                Socket socket = new Socket("127.0.0.1", 7);
                                socket.close();
                               } catch (Exception e) {
                                System.out.println(e);
                               }
                               long end = System.currentTimeMillis();
                               System.out.println("time " + (end - start));
                           }
                          }
                      }
                      
                      /*********************************************************/
                      
                      #include <stdio.h>
                      #include <sys/types.h>
                      #include <sys/socket.h>
                      #include <netinet/in.h>
                      
                      #define PORT 7
                      
                      int main(int argc, char **argv)
                      {
                          int sock;
                          struct sockaddr_in addr;
                          int n;
                          time_t start_time, end_time;
                      
                          start_time = time(NULL);
                      
                          for (n = 0; n < 10; n++) {
                           if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                               perror("socket");
                               exit(1);
                           }
                      
                           addr.sin_family = AF_INET;
                           addr.sin_addr.s_addr =
                               htonl(127 * 256 * 256 * 256 +
                                  0 * 256 * 256 +
                                  0 * 256 +
                                  1);
                           addr.sin_port = htons(PORT);
                      
                           if (connect(sock, &addr, sizeof(addr)) == -1) {
                               perror("connect");
                               /* No we don't exit(1); */
                           }
                      
                           close(sock);
                          }
                      
                          end_time = time(NULL);
                          printf("time %d seconds\n", end_time - start_time);
                          return 0;
                      }
                      • 23. Re: How to write a ping program in java
                        456795
                        In the meantime I am going to see if I can find a
                        windows program that will do tcp echo and see if the
                        times are different.
                        Verified. My ping times are now 0 when I enabled the port for echo reply (port 7).

                        For WindowsXP you have to enable Simple TCP/IP Services to turn on echo. If anyone cares I can give instructions on how to do it.
                        • 24. Re: How to write a ping program in java
                          456795
                          isReachable() tries to connect to port 7 (echo). It
                          doesn't use ICMP ECHO (= ping) on Windows because,
                          according to comments in the source, "Windows
                          implementation of ICMP & RAW sockets is too
                          unreliable for now."
                          Okay I believe that, then what does Windows implementation do after trying port 7, because isReachable() still returns true.
                          • 25. Re: How to write a ping program in java
                            807590
                            what does Windows
                            implementation do after trying port 7, because
                            isReachable() still returns true.
                            It looks at the error it gets. Host unreachable, network unreachable, network is down, protocol family unsupported, and timeout are interpreted as false.

                            Connection refused means "you tried to connect to a port that has no listener". If we get that it means there is a computer that sent us the "connection refused" packet. Ergo, the computer exists and is reachable, and isReachable() returns true.
                            • 26. Re: How to write a ping program in java
                              796365
                              @ChuckBing. Do you have port 7 (echo) open on your box?
                              No, I just checked it at www.grc.com and it showed as it should, "stealthed" - meaning
                              I give no repy - neither accept not deny the connection. It's as if my ip doesn't exist.
                              • 27. Re: How to write a ping program in java
                                796365
                                I am on win XP Pro, btw.
                                • 28. Re: How to write a ping program in java
                                  456795
                                  @ChuckBing. Do you have port 7 (echo) open on your
                                  box?

                                  No, I just checked it at www.grc.com and it showed as
                                  it should, "stealthed" - meaning
                                  I give no repy - neither accept not deny the
                                  e connection. It's as if my ip doesn't exist.
                                  That is because some firewall is blocking external connection to your machine, which is a good thing. Try this at the command line: netstat -a. You should see echo somewhere in that list:
                                  • 29. Re: How to write a ping program in java
                                    456795
                                    . You should see echo somewhere in that list:
                                    More specifically:
                                    UDP    YOUR_MACHINE_HERE:echo         *:*