How to get TCP CLient State

Madz

Centurion
Joined
Jan 22, 2003
Messages
155
I am developing a Network Application i am facing problem , in VB6 Winsock control we have 6 states of Winsock control so we can determinte at which state now socket it. in .net i have implemented a TCPLISTNER server which listnes and connects client , now the problem is that when server is closed, the tcp client still assumes it is connected, how can i check wether this connection is active or not ?
When i write netstat -a it shows me that connection is on Close_Wait , but the TCP Client has only 2 states true or false ,

Please tell me how can i check wether at this time my connection is active or not ?
 
You can retrieve the state from the TcpClient's underlying socket through TcpClient.Client. You'll need to inherit from TcpClient to access this property, since it is protected. The Active property, to which you're referring to, is not updated when the connection is closed by the remote host.
 
Inherited Property

it was exectly the same, coz the active state is also inherited from base class. otherwise it is not accessable through normal code.
Thanks for your help.
 
Still i am unable to get what i want to do , here i develop a class with this code
Code:
Imports System.Net.Sockets

Public Class MyClient
    Inherits System.Net.Sockets.TcpClient

    Public Property MyState() As String
        Get
            Dim s As Socket = Me.Client

            If s.Connected = True Then
                Return "I am Connected"
            Else
                Return "I am not Connected"
            End If
        End Get
        Set(ByVal Value As String)

        End Set
    End Property

End Class

if a from my client i am doing this

Code:
    Dim X As MyClient = New MyClient()

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        X.Connect("localhost", 4896)


    End Sub

    Private Sub cmdStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStatus.Click
        MsgBox(X.MyState)
    End Sub

If i connect to some Server it connects , in case connection is dropped from server then it always shows that this is connected.

I want to be notified when a connection is closed.

can any please provide me some code
 
Problem Solved

Thanks Dear

My Problem has been solved, i used this

Code:
Public Property MyState() As String
        Get

            Dim sck As Socket = Me.Client
            If sck.Poll(1, SelectMode.SelectRead) = False Then
                Return "Still Connected"
            ElseIf sck.Poll(1, SelectMode.SelectRead) Then
                Return ("Lost Connection")
            End If

        End Get
        Set(ByVal Value As String)

        End Set
    End Property


and in application i did this

Code:
console.writeline(Client.myState)

and it returns exect the same what i wanted.
 
Back
Top