Need help using tcpClient and tcpListner

G

Guest

I am having trouble using the TcpListener and TcpClient classes.

At the end of this post is server code that runs, and a class whose purpose
is described below.

I need to know when the client closes the connection so I can exit my
listener code. Here is what I have tried.

First I asked MSDN and they said..

TcpClient creates a Socket to send and receive data over a network. Classes
deriving from TcpClient can use this property to get or set this Socket. Use
the underlying Socket returned from Client if you require access beyond that
which TcpClient provides. You can also use Client to set the underlying
Socket to an existing Socket. This might be useful if you want to take
advantage of the simplicity of TcpClient using a pre-existing Socket.

So, I created a class called MyTcpClient, below, that inherited from
TcpClient. I got this code from the MSDN library. Then I changed all
occurances of TcpClient to MyTcpClient in my code. By doing this, I thought I
could then query the property 'IsConnected'.

The idea here is to be able to get at the 'protected connected' property of
the underlying socket.

However, when I try this, the line of code.

tcpCli = tcpList.AcceptTcpClient()

gives this error during the pre compile phase.


C:\Documents and Settings\hcooper.JOE\My Documents\Visual Studio
Projects\SystemNetDemo\TCPListenerForm.vb(140): Option Strict On disallows
implicit conversions from 'System.Net.Sockets.TcpClient' to
'SystemNetDemo.MyTcpClient'.


so I then insert option strict off at the top of my class. The precompiled
error goes away but when it runs, I get the following error.


An unhandled exception of type 'System.InvalidCastException' occured in
SystemNetDemo.exe
Additional information: Specified cast is not valid.


Any help here???

Hamil.





************************ MyTcpClient class

Imports System.Net.Sockets

Public Class MyTcpClient

Inherits TcpClient

Public IsConnected As Boolean

Public Sub New()
End Sub 'New

Public Sub UsingProtectedMethods()

'Uses the protected 'Active' property belonging to the TcpClient
base class
'to determine if a connection is established.
If Me.Active Then
' Calls the protected 'Client' property belonging to the
TcpClient base class.
Dim s As Socket = Me.Client
'Uses the Socket returned by Client to set an option that is not
available using TcpClient.
IsConnected = s.Connected
End If
'To free all resources, calls protected virtual method Dispose
belonging to the TcpClient base class.
Me.Dispose(True)
GC.SuppressFinalize(Me)
End Sub 'UsingProtectedMethods

End Class



************ This code runs ........

Imports System.IO
Imports System.net
Imports System.Net.Sockets
Imports System.Threading

Public Class TCPListenerForm
Inherits System.Windows.Forms.Form

Dim listening As Boolean
Dim localhostAddress As IPAddress
Dim port As Integer
Dim tcpList As TcpListener
Dim tcpCli As TcpClient
Dim ns As NetworkStream
Dim sr As StreamReader
Dim receivedData As String
Dim returnedData As String
Dim sw As StreamWriter

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
btnStart.Enabled = False
btnStop.Enabled = True
Dim tr As New Thread(AddressOf ListenToClients)
tr.Start()
End Sub

Sub ListenToClients()
localhostAddress = IPAddress.Loopback
port = CInt(txtPort.Text)
tcpList = New TcpListener(localhostAddress, port)
tcpList.Start()
listening = True
Do While listening
Do While tcpList.Pending = False And listening = True
Thread.Sleep(10)
Loop
If Not listening Then Exit Do
Dim tcpCli As New TcpClient
tcpCli = tcpList.AcceptTcpClient()
ns = tcpCli.GetStream
sr = New StreamReader(ns)
sw = New StreamWriter(ns)
tbStatus.Text = "connected"
Do While listening = True
Thread.Sleep(10)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = receivedData.ToUpper
sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
tbStatus.Text = "closed"
Loop
tcpList.Stop()
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStop.Click
btnStart.Enabled = True
btnStop.Enabled = False
listening = False
End Sub
End Class
 
G

Guest

I am withdrawing this question.
As I have read, even the connect property of the socket doesn't give current
status.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top