Jump to content

C++ Ping

- - - - -

  • Please log in to reply
3 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Hello All,

I heard that there is a code for pinging in C++.

#include<windows.h>
int main()
{
  system("ping xxx.xxx.xxx.xxx")
  return 0;
}


So I incorporated it into a new program (not the best, but still serves for a quick application):

[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
#include[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]<windows.h>
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]<iostream>
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] std;
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] main ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])
{
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] ipAdress;
cout<<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"Welcome to Ping Client 1.0 Prototype Alpha."[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];

cout<<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"Please note this is a PROTOTYPE!"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]/n;

cout<<[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"To begin enter the IP Address you want to ping:"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]cin<<ipAddress/n;

system([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515][FONT=Courier New][SIZE=2][COLOR=#a31515]"ping ipAddress"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#a52a2a][FONT=Courier New][SIZE=2][COLOR=#a52a2a][FONT=Courier New][SIZE=2][COLOR=#a52a2a]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
}
[/SIZE][/FONT][/SIZE][/FONT]


I want to know if this will work or not. It won't build (it is the first code I wrote for this program). I want to know how to make whatever number they enter the IP to ping. The ipAddress in the system (ping ipAddress) is the only thing I could put there (I'd like code to enter there that's correct).

Thank You.

#2
deskchecked

deskchecked

    Newbie

  • Members
  • PipPip
  • 29 posts
#include<cstdlib> // 0. stdlib instead of windows.h means it will compile on Linux too. :)
#include<iostream>
#include<sstream> // 1. So we can use stringstream, which will let us format our system() command later

using namespace std; // 2. you needed a space between "using" and "namespace"

int main (void)
{
char target_host[16]; // 3. Your user enters an IP address as a string. "double" is the wrong data type to use here.

cout<<"Welcome to Ping Client 1.0 Prototype Alpha.\n"; // 4. I think you forgot a line feed.

cout<<"Please note this is a PROTOTYPE!\n"; // 5. Backslash, not forward slash for line feeds. Also needs to be *inside* the quotes.

cout<<"To begin enter the IP Address you want to ping:";

cin >> target_host; // 6. Your "arrows" were going the wrong way: we're reading data *from* cin and storing it in target_host.

// 7. Prepare our command for execution.
stringstream cmd;
cmd << "ping ";
cmd << target_host;

// 8. cmd.str() gets a std::string representation of the command. c_str() gets a C string (const char*) representation that system() will accept.
system(cmd.str().c_str());

return 0; // 9. You needed a space between "return" and "0".
}


#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Oh the simple errors line feed, using namespace std, etc.) where made when I pasted in the post. (But thanks anyway)!

Thank You for fixing the syntax and your listed as a Newbie. Are you a Newbie or did you join Codecall recently?

#4
deskchecked

deskchecked

    Newbie

  • Members
  • PipPip
  • 29 posts

Hunter100 said:

Oh the simple errors line feed, using namespace std, etc.) where made when I pasted in the post. (But thanks anyway)!

Had a feeling that might've been the case, but I thought it would be better to be explicit. Be aware that the "cin >> buffer" approach is unsafe: our buffer is 16 chars long, which means it can handle IP addresses of 15 digits (+1 character for a nul terminator). But what happens if the user enters more than 15 characters?

(Hint: read up on buffer overflows!)

Hunter100 said:

Thank You for fixing the syntax and your listed as a Newbie. Are you a Newbie or did you join Codecall recently?

I'm no newbie to C/C++, but yes I joined CodeCall just earlier today. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users