Jump to content

  • Log In with Google      Sign In   
  • Create Account

Ping (Socket)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 31 January 2011 - 05:12 PM

Someone already did the Ping system with Socket?

I need a drip Ip and whether it is responding or not, so simple :)


Sponsor:

#2 hplus0603   Moderators   -  Reputation: 5730

Like
1Likes
Like

Posted 01 February 2011 - 12:28 AM

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 };

#3 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 03 February 2011 - 07:50 AM

Yes, I'm using Windows and need only to know if my host is online or not

I created a simple system like this:
int result;
result = system("ping www.google.com.br");



I'm using it in Qt, but every time you run this code, it is opening the console, how do I notopen the console?

#4 rip-off   Moderators   -  Reputation: 8764

Like
0Likes
Like

Posted 03 February 2011 - 08:36 AM

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.

#5 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 03 February 2011 - 09:22 AM

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.


I need to know only that the host is responding, so I want to use ping Posted Image

Sorry, but the CreateProcess () would be exactly that? what I would use it?

#6 rip-off   Moderators   -  Reputation: 8764

Like
0Likes
Like

Posted 03 February 2011 - 09:40 AM

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.

#7 VitaliBR   Members   -  Reputation: 147

Like
0Likes
Like

Posted 03 February 2011 - 09:55 AM

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.


For accurate test DDNS, whether they are responding (if using DynDNS),

just that

The QProcess worked :D
Thanks

http://purestarman.tistory.com/217




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS