Ping (Socket)

Started by
5 comments, last by VitaliBR 13 years, 3 months ago
[font=arial, sans-serif][size=2]Someone already did the Ping system with Socket?

I need a drip Ip and whether it is responding or not, so simple :)
http://mateusvitali.wordpress.com/
Advertisement
Yes, people have implemented ping systems with sockets.

It's kind of hard to tell from your question what you actually want to do and what you need help with.

Do you have some system ("drip lp" ?) on a network, and you need to monitor this system for whether it is up or not?
If so, does the system already have any kind of network service? A web service? An SNMP interface? Something else?
If the system already has a network service of some sort, doing a TCP connect to that service and verifying that the connect works is going to be the simplest thing.
Doing raw ping (ICMP protocol) packets is likely to be harder, mainly because you need administrator privileges to use raw sockets on modern versions of Windows. (I'm assuming the sending machine is using Windows -- using raw sockets on Linux, Solaris, MacOS, BSD and other OS-es is different).

Here is an implementation of a simple "ping" program using raw sockets on Windows. Remember: it needs an elevated command prompt to work!
#include <winsock2.h>
#include <windows.h>

#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")

int main(int argc, char const *argv[]) {
WSADATA wsi;
memset(&wsi, 0, sizeof(wsi));
if (WSAStartup(MAKEWORD(2, 2), &wsi)) {
printf("cannot open winsock?\n");
return 1;
}
if (argc != 2) {
printf("usage: testping hostname\n");
return 1;
}
int s = socket(AF_INET, 0, IPPROTO_ICMP);
if (s < 0) {
printf("Could not create IPPROTO_ICMP socket: %d\n", WSAGetLastError());
return 1;
}
printf("Looking up %s\n", argv[1]);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
struct hostent *hent = gethostbyname(argv[1]);
if (!hent) {
printf("Could not resolve hostname %s\n", argv[1]);
return 1;
}
unsigned char addr[4];
memcpy(addr, hent->h_addr_list[0], 4);
printf("%s is %d.%d.%d.%d\n", argv[1], addr[0], addr[1], addr[2], addr[3]);
sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, hent->h_addr_list[0], 4);
char icmp[32];
memset(icmp, 0, sizeof(icmp));
icmp[0] = 8; // echo request
icmp[1] = 0;
icmp[2] = 8; // checksum
icmp[3] = 0;
int st = ::sendto(s, icmp, sizeof(icmp), 0, (sockaddr *)&sin, sizeof(sin));
if (st <= 0) {
printf("Could not send ping packet\n");
return 1;
}
char buf[100];
int sl = sizeof(sin);
st = ::recvfrom(s, buf, 100, 0, (sockaddr *)&sin, &sl);
if (st <= 0) {
printf("Error receiving on ping socket\n");
return 1;
}
printf("Got ping response.\n");
return 0;
}
enum Bool { True, False, FileNotFound };
[font="arial, sans-serif"]Yes, I'm using Windows and need only to know if my host is online or not[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]I created a simple system like this:[/font]
[font="arial, sans-serif"]int result;
result = system("ping www.google.com.br");

[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]I'm using it in Qt, but every time you run this code, it is opening the console, how do I notopen the console?[/font]
http://mateusvitali.wordpress.com/
Can't you just try to connect to it? Pinging it doesn't prove its usable, just that it might be contactable. In fact, ping could be blocked while allowing TCP.

Just connect. If you can't, then by all means run a ping if you think it will help the user diagnose the error. You can try use CreateProcess() WinAPI function, which should give you more control over the I/O of the ping program, unlike system(). Some light Googling suggests QT might provide for this case.

Can't you just try to connect to it? Pinging it doesn't prove its usable, just that it might be contactable. In fact, ping could be blocked while allowing TCP.

Just connect. If you can't, then by all means run a ping if you think it will help the user diagnose the error. You can try use CreateProcess() WinAPI function, which should give you more control over the I/O of the ping program, unlike system(). Some light Googling suggests QT might provide for this case.


[font=arial, sans-serif]I need to know only that the host is responding, so I want to use ping [/font]rolleyes.gif
[font=arial, sans-serif]Sorry, but the CreateProcess () would be exactly that? what I would use it?[/font]
http://mateusvitali.wordpress.com/
Responding means different things. Your users won't care if ping works. They care about connecting to the service and doing whatever they want to do. Hence, don't test if ping works, test if the service itself works by using it! If you aren't going to connect to it, then the user doesn't care if it is "responding".

If you want to use Google, pinging www.google.com isn't a great test - it doesn't tell you if you can do a search. It is simpler to connect to www.google.com on port 80, which is a much better test. That said, if the latter fails then trying the former will help you diagnose the issue. For most users they won't care, network connectivity issues are beyond their power to fix.

Tell us, in a bit more detail, why you care that the server responds to a ping request?

CreateProcess() is a function provided by Windows, Google for it to find out more information.

Responding means different things. Your users won't care if ping works. They care about connecting to the service and doing whatever they want to do. Hence, don't test if ping works, test if the service itself works by using it! If you aren't going to connect to it, then the user doesn't care if it is "responding".

If you want to use Google, pinging www.google.com isn't a great test - it doesn't tell you if you can do a search. It is simpler to connect to www.google.com on port 80, which is a much better test. That said, if the latter fails then trying the former will help you diagnose the issue. For most users they won't care, network connectivity issues are beyond their power to fix.

Tell us, in a bit more detail, why you care that the server responds to a ping request?

CreateProcess() is a function provided by Windows, Google for it to find out more information.


[font="arial, sans-serif"]For accurate test DDNS, whether they are responding (if using DynDNS),[/font]
[font="arial, sans-serif"]just that[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]The QProcess worked :D[/font]
[font="arial, sans-serif"]Thanks[/font]
[font="arial, sans-serif"] [/font]
[font="arial, sans-serif"]http://purestarman.tistory.com/217[/font]
http://mateusvitali.wordpress.com/

This topic is closed to new replies.

Advertisement