Need help on System.Net.Sockets.TcpClient


  • Hello everybody


    Here is the source code of my programm


    Imports System

    Imports System.Text

    Imports System.IO

    Imports System.Net

    Imports System.Net.Sockets

    Imports System.Threading


    Public Class Form1

    Dim WithEvents client As TcpClient

    Dim WithEvents b As NetworkStream

    Dim bytes(client.ReceiveBufferSize) As Byte


    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    On Error Resume Next

    client = New TcpClient

    client.Connect("", 23)

    b = client.GetStream


    b.Read(bytes, 0, CInt(client.ReceiveBufferSize))

    Dim returndata As String = Encoding.ASCII.GetString(bytes)

    TextBox1.Text = TextBox1.Text + returndata


    End Sub


    'Problem is here

    Public Sub avail() Handles  b.CanRead 'this is not working

    ' in this "public sub" in want my codes

    End Sub


    End Class


    I know that I has to write some  in this codes,so  "b.CanRead"  is accepted by vb but, I do not know what i have to write.

    Actually I want to use system.net like winsock of vb6.

    Any help?

    Thank's in Advance.



    Saturday, September 13, 2008 9:53 AM


  • NetworkStream.CanRead is the property. If CanRead is true, NetworkStream allows calls to the Read method. Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability of the NetworkStream. The CanRead property is set when the NetworkStream is initialized.

    The following code example checks CanRead to verify that the NetworkStream is readable. It then performs a read operation on the NetworkStream.

    ' Examples for CanRead, Read, and DataAvailable.
    ' Check to see if this NetworkStream is readable.
    If myNetworkStream.CanRead Then
    Dim myReadBuffer(1024) As Byte
    Dim myCompleteMessage As StringBuilder = New StringBuilder()
    Dim numberOfBytesRead As Integer = 0

    ' Incoming message may be larger than the buffer size.
          numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
               myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
       Loop While myNetworkStream.DataAvailable

    ' Print out the received message to the console.
           Console.WriteLine(("You received the following message : " + myCompleteMessage.ToString()))
       Console.WriteLine("Sorry.  You cannot read from this NetworkStream.")
    End If

    Tuesday, September 16, 2008 8:49 AM