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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!


Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.