RE: Need help using tcpClient and tcpListner

From: hamil (hamil_at_discussions.microsoft.com)
Date: 10/14/04


Date: Wed, 13 Oct 2004 21:11:02 -0700

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

"hamil" wrote:

> 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
>



Relevant Pages

  • Re: Multithreading the Winsock Control
    ... The key to this is putting the server socket into a control array and giving ... Private Sub sckServer_ConnectionRequest(Index As Integer, ... Dim msg As String ... Dim Source As String ...
    (microsoft.public.vb.controls)
  • Re: Socket and ThreadPool
    ... What .NET socket component that I can use that has good performance? ... but when the client send a message to me, the Sub SocketClient_OnRead ... Private Sub ListenButton_Click(ByVal sender As System.Object, ... Dim strLocalAddress As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Socket and ThreadPool
    ... Microsoft Winsock control is slower than a 3rd party control. ... What .NET socket component that I can use that has good performance? ... but when the client send a message to me, the Sub SocketClient_OnRead ... Dim strLocalAddress As String ...
    (microsoft.public.dotnet.languages.vb)
  • Still Cant Get Form.show with socket
    ... I have it working with Winsock, but would like to be able to use the socket ... Private SocketChatC As TcpClient ... Private Sub DoListen() ... Dim Ipep As New IPEndPoint ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Tcp/Ip echo speed
    ... The second program is the TcpClient. ... Imports System.net ... Dim localhostAddress As IPAddress ... End Sub ...
    (microsoft.public.dotnet.languages.vb)