Thread: How to deal with ping timeouts?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    How to deal with ping timeouts?

    In my app whenever a button is pressed, a function starts checking the state of some Checkboxes and Edit Controls and start X (up to 10) threads to ping the host adresses that were in this Edit Controls. Up to now things are going great, only problem is that I donīt know how to deal with a timeout, my program is waiting forever for the replies.

    I though of creating a Windows Timer, once the Time specified in the TimeOut Field is reached it reads the ping buffer: If thereīs nothing there the host was not found. Else it was a TimeOut. Then I would kill the thread.

    But I am almost sure that is not the most correct way to do it, there might be some smarted way that iīm not aware off. I think itīs not very correct either to just go killing threads like that, without letting it free itīs resources when it exits correctly right?

    Does anyone has any idea that could help me? Any help will be great, Thanks!

    PS: Iīm using MS ICMP API, loading 'icmp.dll' and calling IcmpSendEcho.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    Just a small comment... You code god already know the structure of IcmpSendEcho, anyway here it is:
    Code:
    //From MSDN
    DWORD IcmpSendEcho(
      HANDLE IcmpHandle,
      IPAddr DestinationAddress,
      LPVOID RequestData,
      WORD RequestSize,
      PIP_OPTION_INFORMATION RequestOptions,
      LPVOID ReplyBuffer,
      DWORD ReplySize,
      DWORD Timeout
    );
    Parameters
    IcmpHandle
    [in] Open handle returned by IcmpCreateFile.
    DestinationAddress
    [in] Specifies the destination of the echo request.
    RequestData
    [in] Specifies the buffer that contains the data to send in the request.
    RequestSize
    [in] Specifies the number of bytes in the request data buffer.
    RequestOptions
    ReplyBuffer
    [out] Buffer to hold any replies to the request. Upon return, the buffer contains an array of ICMP_ECHO_REPLY structures followed by the options and data for the replies. The buffer should be large enough to hold at least one ICMP_ECHO_REPLY structure plus MAX(RequestSize, 8) bytes of data since an ICMP error message contains 8 bytes of data.
    ReplySize
    [out] Receives the size in bytes of the reply buffer.
    Timeout
    [out] Recieves the time in milliseconds to wait for replies.

    Return Values
    Returns the number of ICMP_ECHO_REPLY structures stored in ReplyBuffer. The status of each reply is contained in the structure. If the return value is zero, see the extended error information available through GetLastError.

    So, what my lame brain allows me to understand is that if thereīs some reply in a time lower than the Timeout value, the function will return immediately after this reply and the return value will be '1' because thereīs ONE reply in the buffer and if thereīs no reply and the Timeout value is reached the function will return immediately at the Timeout time and the return value will be '0' because there is no reply in the buffer. Do this really works like that? Or am I misunderstanding everything?

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Seems you have the idea.

    Could be the return contains more than one struct.

    If it returns zero call GetLastError() or WSAGetLastError() to confirm it was a time out rather than some other error (GetLastError() should return ERROR_TIMEOUT or 1460)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    Thanks for the reply Novacain, I think Iīm getting it now
    Let me abuse of your patience for a while, i just noticed this at MSDN description of IcmpSendEcho():

    Requirements
    Client: Included in Windows XP, Windows 2000 Professional.
    Server: Included in Windows Server 2003, Windows 2000 Server.

    That canīt be, Iīm on a WinME box and Iīm watching the function work... As a matter of fact, I did a 'pexports icmp.dll > icmp.def' and I found the function there... But I now realized that I was taking for granted that this function would be available in ANY MS OS since W95... As I canīt just rely on MSDN info (which is incorrect), how the hell will I know which MS OSīs support it?

    Do you think it would be better to create my own DLL maybe based on RAW sockets and include it with my app? Or is it safe to rely on icmp.dll?

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Depends on how often you think your app will run on a MS OS made before ME. Also how critical this section of the app is.

    I am pretty sure it works in WIN98 SE as well or could be just anyone who has upgraded / patched their winsock.

    Your app could search for the DLL, test the OS version and warn if incompatable or create your own DLL
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ping script doesn't work?
    By userpingz in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 07:53 PM
  2. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  3. IPv6 ping in windows...problem..lots of ode:(
    By Neill KElly in forum C Programming
    Replies: 3
    Last Post: 04-27-2009, 11:50 PM
  4. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  5. ping client
    By cpp_is_fun in forum C Programming
    Replies: 4
    Last Post: 11-29-2006, 12:44 PM