473,756 Members | 8,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How detect LAN disconect

Hi

I have application where I used tcpListener to connect with another system.
Every thing is ok that it's time to error handling
and I have problem
when the TCP client is disconect I don't know how detect this situation
the same situation is when I plug out patch-cord

For waiting for a data I use this loop
While Me.mSocket.Conn ected
Try
Thread.Sleep(10 0)
If Me.mSocket.Avai lable > 0 Then
Dim lBuffer(Me.mSoc ket.Available) As Byte
Me.mSocket.Rece ive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASC IIEncoding.ASCI I.GetString(lBu ffer)
RaiseEvent DataReceived(ms trDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End While

all the time mSocket(TcpList ener) has Connected = True even when I disconect

How I can detect this situation?

Regards
Szafranr
Nov 21 '05 #1
3 4278
You can make use of WMI; look at the Win32_NetworkAd apter class for this.
Use the classes under System.Manageme nt namespace to retrieve the network
adapter information.

for instance, the following code will loop through all network adapters
listing their properties:

Dim oQuery As New Management.Sele ctQuery("Win32_ NetworkAdapter" )
Dim oObjSearcher As New Management.Mana gementObjectSea rcher(oQuery)
Dim oBaseMgmtObj As Management.Mana gementBaseObjec t

For Each oBaseMgmtObj In oObjSearcher.Ge t()
For Each oProperty as Management.Prop ertyData in
oBaseMgmtObj.Pr operties()
Debug.WriteLine ("Name: " & oProperty.Name & vbTab & "Value: " &
oProperty.Value )
Next
Next oBaseMgmtObj

the properties applicable to the Win32_NetworkAd apter object are listed
here:
http://msdn.microsoft.com/library/de...orkadapter.asp

one of the properties is the 'NetConnectionS tatus' which you can look at to
determine whether the adapter
is disconnected or connected or something else. You can look up these values
from the link above.

hope this helps..
Imran.

"Szafranr" <sz**********@M poczta.onet.pl> wrote in message
news:cf******** **@news.onet.pl ...
Hi

I have application where I used tcpListener to connect with another system. Every thing is ok that it's time to error handling
and I have problem
when the TCP client is disconect I don't know how detect this situation
the same situation is when I plug out patch-cord

For waiting for a data I use this loop
While Me.mSocket.Conn ected
Try
Thread.Sleep(10 0)
If Me.mSocket.Avai lable > 0 Then
Dim lBuffer(Me.mSoc ket.Available) As Byte
Me.mSocket.Rece ive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASC IIEncoding.ASCI I.GetString(lBu ffer)
RaiseEvent DataReceived(ms trDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End While

all the time mSocket(TcpList ener) has Connected = True even when I disconect
How I can detect this situation?

Regards
Szafranr

Nov 21 '05 #2
> one of the properties is the 'NetConnectionS tatus' which you can look at
to
determine whether the adapter
is disconnected or connected or something else. You can look up these values from the link above.


Your example can detect when patch-cord is plug off.
I still don't know how I can detect when TCP client close connection

For instance
Server: create tcpListener with specific port (30000)
Client: connect to Server:30000
Server: start the loop when S waiting for data

While tcpListener.Con nected
Try
Thread.Sleep(10 0)
If Me.mSocket.Avai lable > 0 Then
Dim lBuffer(Me.mSoc ket.Available) As Byte
Me.mSocket.Rece ive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASC IIEncoding.ASCI I.GetString(lBu ffer)
RaiseEvent DataReceived(ms trDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End While

Client: Close connection
Server: tcpListener.Con nected = true and loop still works
In this situation I have to detect the tcpClient closed connection from
socket

Regards
Szafranr
Nov 21 '05 #3
Well, your topic said 'lan disconnect' so that is what I gave you.
Anyway, the connected property of the socket class is not a sure shot way to
determine whether the socket is still connected to a remote resource or not.
read the notes in the MSDN documentation here:
http://msdn.microsoft.com/library/de...ectedtopic.asp

you might want to try what the documentation suggests. The idea is to catch
the appropriate exception when data cannot be sent/received and then
determine whether this was because of the socket being disconnected or
something else. I know this was probably not the answer you were expecting
but since I haven't use sockets in .net as such, I'm not aware of the best
way to handle such a situation, i.e., socket being disconnected.

hope this helps..
Imran.

"Szafranr" <sz**********@M poczta.onet.pl> wrote in message
news:cf******** **@news.onet.pl ...
one of the properties is the 'NetConnectionS tatus' which you can look at

to
determine whether the adapter
is disconnected or connected or something else. You can look up these

values
from the link above.


Your example can detect when patch-cord is plug off.
I still don't know how I can detect when TCP client close connection

For instance
Server: create tcpListener with specific port (30000)
Client: connect to Server:30000
Server: start the loop when S waiting for data

While tcpListener.Con nected
Try
Thread.Sleep(10 0)
If Me.mSocket.Avai lable > 0 Then
Dim lBuffer(Me.mSoc ket.Available) As Byte
Me.mSocket.Rece ive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASC IIEncoding.ASCI I.GetString(lBu ffer)
RaiseEvent DataReceived(ms trDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToStr ing)
End Try
End While

Client: Close connection
Server: tcpListener.Con nected = true and loop still works
In this situation I have to detect the tcpClient closed connection from
socket

Regards
Szafranr

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4587
by: R. Rajesh Jeba Anbiah | last post by:
Here is a nice code to detect utf-8 <http://in2.php.net/utf8_encode#39986> But, I couldn't find out the logic behind the script. If anyone knows that please share. Particularly I would like to detect other encodings too. So, I would like to know the logic. For example these texts are in TSCII encoding (for Tamil): Žì¸õ. þÐ ¾Á¢Æ¢ø ¯ûÇ Åâ. þ¨¾ ¯í¸Ç¡ø ÀÊì¸ ÓÊó¾¡ø, ¯í¸û ¯Ä¡Å¢Â¢ø ±ó¾...
23
6530
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I know if Window.Open has been redefined? BACKGROUND:
23
15599
by: Michel Bany | last post by:
I am trying to parse responseXML from an HTTP request. var doc = request.responseXML; var elements = doc.getElementsByTagName("*"); the last statement returns an empty collection when running from IE6. It returns the expected collection when running under Firefox or Mozilla. Anybody can explain ? Michel.
10
25634
by: Ben Xia | last post by:
Is there some way can detect MAC address with PHP? any help will be appreciate. Ben
6
5242
by: hb | last post by:
Hi, Would you please tell me how to detect if the client's browser is closed? I need such event to trigger a database modification. Thank you hb
12
17060
by: Patrick Dugan | last post by:
I have an vb.net application that is a module that uses a "application.run" in the sub main to start. There is no form involved (just a system tray icon) How can you detect when the application is being closed? It is easy enough if the user selects "exit" from this tray icon but how can you detect if Windows is closing the program down? Normally I would simply do something in the Form.Closing event but without a form how can you...
4
2819
by: Chris Johnson | last post by:
Hey all, I have a small app I have developed that periodically pings a list of server and returns their status. Given my environment I have setup the program such that a server can be down 4 minutes before I get an alert. My problem now is that is a server is rebooted, it will be back up before the 4 minute window and I will not be aware that it has rebooted. I am looking for a way (as an administrator, or not, if possible) to detect
4
7182
by: goscottie | last post by:
I used submodal as my popup window. With some tweaks, it working great in my app. However, I can't find a way to detect session timeout in the popup window. The app is a form based authentication so the default.aspx page will show up in the popup. So I'm looking into several options. 1. detect session timeout in javascript so that I prevent the popup from even happening. I can simply redirect or post to itself so that the calling...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.