Pinging a Specified Port

Hello all,
I am wondering if anyone has a working setup for pinging a specific port number at a target device.

I have used various Arduinos and Ethernet shields with the ICMPPing library, but I have yet to find any support for pinging ports (other than 80) there or elsewhere in the Arduino community. Any direction would be appreciated.

ICMP (including Ping) is part of the Networking layer, TCP/UDP port definitions are part of the Transport layer. So you can't ping to a port.
You can however, try to connect to a TCP port. If the connection is refused, you know that that port is not listening for connections.

Pieter

Interesting... I can successfully ping my intended target port using Windows, what am I missing here?

For more info, I am pinging a device on a remote router. The router is set up to forward specific ports to the target device. The router ignores pings, but I get pings returned (using windows command line) from the target device's port. I would like to do this from my Mega instead.

I can make an HTTP request of the target Arduino, but it is already running a small server, so I would like to just ping it.

finchamp:
Interesting... I can successfully ping my intended target port using Windows, what am I missing here?

That would surprise me. You can't ping a port. Period. But you can check whether a port is open or not.

Welp, go to know... Thanks for taking the time to straighten me out.

Do you have a suggestion of how I should connect without causing the server (target) to reply with its web page every time?

You could just connect, don't send anything to the server, and disconnect again.
Or if you want to make sure that the server is running, and it is hosting the resource you need, you can use HTTP HEAD.

Thanks again. Of course it would have to be simple too, wouldn't it? :slight_smile: I will try to post working code here for posterity when I have it.

Here's what I've got so far. Open to suggestions, but at this moment it works as intended.

//necessary declarations
#include <EthernetClient.h>
#include <Streaming.h>  //http://arduiniana.org/libraries/streaming/  This library just provides the macro for streaming Serial commands (and all functions that inherit from the print class) as below. Use it or don't.
EthernetClient client2;
float Ping(IPAddress target, uint16_t port = 80);  //overloaded such that function calls with no port number supplied default to 80
uint32_t currentmillis = 0;


float Ping(IPAddress target, uint16_t port) {  //"Pings" an open remote port by simple connect/disconnect and returns approx. round trip time (or -1 if failed)

  Serial << F("Pinging ") << target << endl;  //comment this out to ping silently
  float PingTime = 0;
  currentmillis = millis();
  uint32_t PingStart = currentmillis;

  if (client2.connect( target, port )) {

    currentmillis = millis();
    Serial.print(F("Ping time: "));                //comment this out to ping silently
    PingTime = currentmillis - PingStart;      //calculates the time elapsed to this point
    Serial.print(PingTime);                         //comment this out to ping silently
    Serial.println(F("ms"));                         //comment this out to ping silently

  } else {

    Serial.println(F("Ping failed!"));
    PingTime = -1;

  }

  client2.stop();
  return PingTime;

}

Thank you for reporting back your solution!

Thanks! I hope it saves someone else the research time.