PC Review


Reply
Thread Tools Rate Thread

How detect LAN disconect

 
 
Szafranr
Guest
Posts: n/a
 
      8th Aug 2004
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


 
Reply With Quote
 
 
 
 
Imran Koradia
Guest
Posts: n/a
 
      9th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:cf5uji$l9q$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Szafranr
Guest
Posts: n/a
 
      9th Aug 2004
> 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


 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      9th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:cf7civ$efs$(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
auto disconect DEREK VOSPER Windows XP Help 3 18th May 2004 10:50 AM
browser disconect mark Windows XP Internet Explorer 1 6th Apr 2004 11:16 PM
cannot disconect =?Utf-8?B?cmV4?= Windows XP Help 1 12th Mar 2004 04:40 PM
Server disconect Daig40 Microsoft Windows 2000 Networking 1 6th Mar 2004 12:21 AM
Automatic disconect Tony Windows XP General 3 31st Oct 2003 04:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:37 AM.