Board index » cppbuilder » Re: How to detect if network cable is unplugged? - no more

Re: How to detect if network cable is unplugged? - no more


2004-02-19 05:19:46 PM
cppbuilder94
Quote
Stop trying to use VB example code (especially .NET code,
which "System.Management" belongs to), it is not C++.
sorry. :>
Quote
IWbemLocator interface and go from there. For example (untested):
thanks for the sample. appreciate it very much.
i did make some changes to make run, but it doesn't seem to
be working...
Quote
VARIANT Value;
i dont know how to use this VARIANT thing,
i tried this though AnsiString str = Value.bstrVal;
and got a null, sorry i havent encountered a VARIANT before.
but i did Debug-Inspect "Value" and compared its contents
when my net-cable is plugged and when it isn't,
and the contents didn't changed.
so im assuming that it didn't work the way it should have.
Maybe, i should be dropping this kind of solution to my prob.
My original problem, is that i need to know when a client
pc is no longer connected to my pc or to a network.
I have this server program, that uses tcp. (sorry, but
i cant modify the client program, its not mine).
the server program, simply waits for messages from the client
using a TThread for serving.
What i want to do, is to create a separate thread that
checks if the client pc is still working (connected),
so that i can terminate the server thread when i finds
out that there really is no client to listen to.
because, i always know the ip address of the client pc,
i tried using gethostbyaddr() to periodically check if
there's still the client.
this works, yes, but the function is simply too slow.
do you have alternatives?
do i need to completely drop this thing about wmi?
any ideas?
any help would be great!
Happy Programming! :)
 
 

Re:Re: How to detect if network cable is unplugged? - no more

"mickey" < XXXX@XXXXX.COM >wrote in message
Quote
i dont know how to use this VARIANT thing,
A Variant is basically nothing more then a structure that contains a vt
member to specify what kind of data type is contained, and a union of
possible data values. You look at the vt member to determine the data type,
and then access the appropriate data member. For example, if the vt is
VT_BSTR then you use the bstrVal member. If it is VT_I4, you use the lVal
member instead, and so on.
Alternatively, have a look at the VCL's Variant or TVariant wrapper classes,
which handle thise details for you.
Quote
but i did Debug-Inspect "Value" and compared its
contents when my net-cable is plugged and when it
isn't, and the contents didn't changed.
What OS are you actually using? Only Win2K and XP support detecting when
the network cable is pulled out.
Quote
I have this server program, that uses tcp. (sorry, but
i cant modify the client program, its not mine).
the server program, simply waits for messages from
the client using a TThread for serving.
If the server is yours, then one thing you can do, depending on what kind of
communications are actually involved, is to simply write to the client and
see if it responds. If you don't get a respond after so many seconds,
simply disconnect the connection. Essentially implementing your own timeout
ping mechanism.
Gambit
 

Re:Re: How to detect if network cable is unplugged? - no more

thanks Remy for the reply.
Quote
Alternatively, have a look at the VCL's Variant or TVariant wrapper classes,
yes, i will. im intereted in knowing this data type (or struct).
so i guess i can assign without problems a VARIANT to a
TVariant then for wrapping?
TVariant = Value; //VARIANT value;
Quote
What OS are you actually using? ...Win2k, WinXP
yes, im using Win2k
Quote
simply write to the client and see if it responds.
the problem with my situation is the sequence of messages.
only the client can initiate a communication sequence with
my server. so, the client sends a message, i recieve it,
then i send a reply. i cant do it the other way around.
i can only send a reply to a recieved message, if i send
a different message, the client treats it as unknown reply,
and considers the connection troublesome. even if it
does accept my message, it will not send the proper reply.
am i making sense here?
im sorry, im really starting to learn the client program,
and im new to tcp programming.
Quote
...timeout ping mechanism.
i was able to create a thread that pings the client ip
(thru icmp dll). i placed it on a TThread and created
a TTimer in it. On the timer event, i ping the client.
if the ping doesnt work properly, then the client or the
connection is not online
this is working pretty well, except im worried that
this might cause much slowdowns.
i.e. i set the TTimer interval to 5000ms,
and so every 5 seconds 2 ping packets are transferred
(when connected).
the network connection status icon on my taskbar keeps
lighting up.
is this a good idea?
Happy Programming! :)
 

{smallsort}

Re:Re: How to detect if network cable is unplugged? - no more

"mickey" < XXXX@XXXXX.COM >wrote in message
Quote
so i guess i can assign without problems a VARIANT to a
TVariant then for wrapping?
TVariant = Value; //VARIANT value;
Yes.
Quote
only the client can initiate a communication sequence with
my server. so, the client sends a message, i recieve it,
then i send a reply. i cant do it the other way around.
What about making the client send a ping type of messages periodically?
Then have the server reply to them when received. If your server does not
receive any messages from the client for awhile, just disconnect the client.
The same can be done to let the client determine whether the server is still
responding - if the client sends a ping but does not receive a response then
the client can disconnect from the server and reconnect later.
Quote
i was able to create a thread that pings the client
ip (thru icmp dll).
I did not mean "ping" in the literal sense. I meant just normal commands
that fit in your existing communication protocol that are sent back and
forth between the client and server periodically on the already-established
connection to make sure that both parties are still available and paying
attention to each other.
Quote
i placed it on a TThread and created a TTimer in it.
Do not use TTimer inside a worker thread. Just use a Sleep() instead, or a
waitable timer via CreateWaitableTimer() and WaitForSingleObject().
Gambit
 

Re:Re: How to detect if network cable is unplugged? - no more

thanks again remy for the reply.
Quote
What about making the client send a ping type of messages periodically?
I wish i could do that! but i really cant.
you see, im not supposed <or best avoid>doing something
about the client pc. the client program is already an exe,
and id rather not run a separate program on the client pc.
but no worries for me, coz the thread with the Waitabletimer
is now working right.
Quote
I did not mean "ping" in the literal sense.
ooopppsss! sowee. hahaha :)
Quote
Do not use TTimer inside a worker thread.
yeah, when i tried to suspend the thread, i got an error.
<though im not sure it its entirely my fault :)>
could you give me a short explanation why i cant use
a ttimer? <coz i really have no idea why>
here's how i implemented the waitable timer,
this is from bits and pieces of sample codes i found:
############################################################
//in a function to prepare the thread
hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
LARGE_INTEGER lElapse; //i dont know what this is
int elapse = iInterval;
iElapse.QuadPart = - ((int)elapse * 10000);//???
BOOL succ = SetWaitableTimer(hTimer, &lElapse, elapse,
NULL,//TimerProc, this, FALSE);
bEnabled = succ;
//on the thread's execute()
while (bEnabled && !Terminated) {
WaitForSingleObject(hTimer, 5000);
checkClientConnection(); //my ping function
}
CancelWaitableTimer(hTimer);
CloseHandle(hTimer);
###########################################################
did i do it right?
anyway, thanks for all the help gambit! :)
 

Re:Re: How to detect if network cable is unplugged? - no more

"mickey" < XXXX@XXXXX.COM >wrote in message
Quote
I wish i could do that! but i really cant.
Then you are out of luck, as there is no single reliable way to do what you
ask otherwise. Not all systems can detect the network cable being pulled
out, and the underlying socket stack itself usually doesn't detect
prematurely-lost connections for awhile. That is why you need to implement
some of timeout system in your own code to handle the situations better than
the machine itself can.
Quote
you see, im not supposed <or best avoid>doing
something about the client pc. the client program is
already an exe, and id rather not run a separate
program on the client pc.
I did not say anything about running a separate program. Just have the
existing client program updated to support sending the new command
periodicially.
Quote
could you give me a short explanation why i cant use
a ttimer? <coz i really have no idea why>
Because TTimer is based on the WM_TIMER message, and thus requires the
owning thread to have an active message queue. Threads do not have message
queues initially, so you would have to implement one yourself in your worker
thread.
Gambit