By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

How detect LAN disconect

P: n/a
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.Connected
Try
Thread.Sleep(100)
If Me.mSocket.Available > 0 Then
Dim lBuffer(Me.mSocket.Available) As Byte
Me.mSocket.Receive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASCIIEncoding.ASCII.GetString(lBuffer)
RaiseEvent DataReceived(mstrDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While

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

How I can detect this situation?

Regards
Szafranr
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can make use of WMI; look at the Win32_NetworkAdapter class for this.
Use the classes under System.Management 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.SelectQuery("Win32_NetworkAdapter")
Dim oObjSearcher As New Management.ManagementObjectSearcher(oQuery)
Dim oBaseMgmtObj As Management.ManagementBaseObject

For Each oBaseMgmtObj In oObjSearcher.Get()
For Each oProperty as Management.PropertyData in
oBaseMgmtObj.Properties()
Debug.WriteLine("Name: " & oProperty.Name & vbTab & "Value: " &
oProperty.Value)
Next
Next oBaseMgmtObj

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

one of the properties is the 'NetConnectionStatus' 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**********@Mpoczta.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.Connected
Try
Thread.Sleep(100)
If Me.mSocket.Available > 0 Then
Dim lBuffer(Me.mSocket.Available) As Byte
Me.mSocket.Receive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASCIIEncoding.ASCII.GetString(lBuffer)
RaiseEvent DataReceived(mstrDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While

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

Regards
Szafranr

Nov 21 '05 #2

P: n/a
> one of the properties is the 'NetConnectionStatus' 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.Connected
Try
Thread.Sleep(100)
If Me.mSocket.Available > 0 Then
Dim lBuffer(Me.mSocket.Available) As Byte
Me.mSocket.Receive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASCIIEncoding.ASCII.GetString(lBuffer)
RaiseEvent DataReceived(mstrDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While

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

Regards
Szafranr
Nov 21 '05 #3

P: n/a
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**********@Mpoczta.onet.pl> wrote in message
news:cf**********@news.onet.pl...
one of the properties is the 'NetConnectionStatus' 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.Connected
Try
Thread.Sleep(100)
If Me.mSocket.Available > 0 Then
Dim lBuffer(Me.mSocket.Available) As Byte
Me.mSocket.Receive(lBuffer)
If lBuffer.Length > 0 Then
mstrDataIn =
System.Text.ASCIIEncoding.ASCII.GetString(lBuffer)
RaiseEvent DataReceived(mstrDataIn)
mstrDataIn = ""
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While

Client: Close connection
Server: tcpListener.Connected = 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 discussion thread is closed

Replies have been disabled for this discussion.