Pinging from a C/C++ program
Asked Answered
G

4

43

I want to write a C or C++ program, that given an IP address, Pings it and then performs further action based on whether the Ping was successful or not. How to do this?

Gherkin answered 14/7, 2011 at 17:14 Comment(2)
Depending on what you want to accomplish, the nmap sources may be interesting to look through.Outing
Duplicate: #8190435Absolutely
V
28

Have a blast at The Ping Page, which has a link to full source on the original Unix ping(8).

Vicereine answered 14/7, 2011 at 17:17 Comment(1)
Thanks! It has a lot of legacy code that makes it not work properly with modern C compilers. Is there an updated source code for ping?Gossip
A
19

EDIT I saw after I posted, you are on Ubuntu. However someone searching this question may still find these links helpful for Windows.

Ping: Raw Sockets Method: http://tangentsoft.net/wskfaq/examples/rawping.html

Implementing Internet Pings Using Icmp.dll: http://support.microsoft.com/default.aspx?scid=kb;en-us;170591

IcmpSendEcho Function: http://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx

Ping for Windows: http://www.codeproject.com/KB/IP/winping.aspx

Aphonia answered 14/7, 2011 at 20:36 Comment(0)
D
5

This post is old but I think the following link will help future people lookign for a good explanation on how to create a Ping request.

Deference answered 7/7, 2016 at 7:55 Comment(0)
D
4
#include <iostream>
using namespace std;
int main() {
int x = system("ping -c1 -s1 8.8.8.8  > /dev/null 2>&1");
if (x==0){
    cout<<"success";
}else{
    cout<<"failed";
}

replace 8.8.8.8 from your IP Address

Dippy answered 7/10, 2020 at 8:23 Comment(1)
That is *nix-specific (wouldn't work on Windows)Flay

© 2022 - 2024 — McMap. All rights reserved.