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?
Asked
Active
Viewed 8.7k times
40
-
Depending on what you want to accomplish, the nmap sources may be interesting to look through. – dascandy Jul 14 '11 at 17:40
-
Duplicate: http://stackoverflow.com/questions/8189935/is-there-any-way-to-ping-a-specific-ip-address-with-c/23071412#23071412 – Leo Rohr Jul 16 '15 at 22:23
4 Answers
27
Have a blast at The Ping Page, which has a link to full source on the original Unix ping(8).
Nikolai Fetissov
- 79,853
- 11
- 109
- 168
17
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
T.T.T.
- 31,761
- 47
- 124
- 165
4
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.
David Gatti
- 3,347
- 2
- 29
- 62
3
int main() {
int x = system("ping -c1 -s1 8.8.8.8 > /dev/null 2>&1");
if (x==0){
cout<<"success"<<endl;
}else{
cout<<"failed"<<endl;
}
replace 8.8.8.8 from your IP Address
Nuwan madhusanka
- 51
- 3