Is there any event that triggers disconnections of the pc's cable from the hub that connect it to the internet ?

What about a procedure like "IsAlive"?

Thank in advance

Who is Participating?
ergatesConnect With a Mentor Commented:
Although the network interface card may be able to detect a hardware disconnection, I know of no interface that would make this info available to an application.

In general at the TCP socket level you can only try doing something and see if it times out.

An "IsAlive" could do a TCP connection or maybe a "ping" to a reliable host on the other side of the connection, e.g an internet router. If you get no response then the connection may be gone.

There is a freeware component on the Delphi Super Page in the file icmp.zip, which has a ping component and some sample code.


Timeouts for socket streams are matter of minutes.

To check for a disconnection of the peer, you will just write to the socket at custom intervals and look for a broken pipe.

Rgds, julio
Although socket timeouts are relatively long, you can always run your own timer, and not rely on the comms. It seems easier to do a connect, rather than find a host you can keep sending data to.

If you want response times down to seconds, I would recommend the ICMP ping method. I noticed an applet on Delphi Super Page which uses the icmp component to give a real time status display of a number of hosts, could be fun!


Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ronit051397Author Commented:
I wand to use the function "SetSockOpt" and change the "KeepAlive" option to true, do you have an example source ?
Sorry, but ping is definitely not a choice:
you could ping (or connect, in general) to know if there's anybody on the other side, still you won't know if _your_ program's connection is alive.
In fact, an "IsAlive" does not do a TCP connect or ping.

keep in mind that, anycase, a socket would time out in about 15-20 minutes, depending on the OS... are you prepared to wait so long?

I would strongly suggest the Socket FAQ (somewhere on-line): it's an invaluable source of information.

Bye, julio
ronit051397Author Commented:
Thanks Julio

Do you know how can I use the "SetSockOpt" Function and change the "KeepAlive" flag?
This is the sort of code I use in my socket component:

   err : integer;
   optval : integer;
   sock : socket;
   optval := 1;
   err := setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, @optval, sizeof(optval));
   if err = SOCKET_ERROR then
      err := WSAGetLastError;

However, the KEEPALIVE timeout is by default 2 hours (see RFC 1122). I had thought "IsAlive" was a hypothetical procedure, were you referring to a real procedure?

The question you asked was how to detect if a PC was disconnected from the network, not if a particular TCP connection is disconnected. As I said in my original answer, the way to detect if a TCP connection is alive is to send some data and see if you get a response. See RFC 1123 for discussions of how applications such as SMTP should operate timeouts. TCP timeouts are typically 2-4 minutes (RFC 0793). If you can't wait that long you can run your own timer.

I could write a book about how TCP/IP protocols work, but basically it is up to the client application and server to ensure their connection is operating correctly. The underlying layers will not tell you the connection is lost, because they simply don't know.

I rarely need to look at FAQs, I have been writing comms software for 10 years, and I go to the specifications (RFCS, Winsock spec) if I need to look up something.

There is a Winsock FAQ at the Stardust site, http://www.stardust.com/wsresource/wsresrce.html, but it doesn't mention keepalive functions. There is also an FAQ for Unix/C programmers at http://prominence.com/books/net/cd/socket/unix-socket-faq.html.

What is the application you are writing, and why do you need to detect disconnection? Also let me know where you have seen this "IsAlive" function.

ronit051397Author Commented:
Thanks ergates and julio for your answers it was helpfull.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.