get length of data sent over network to TCPlistener/networkstream vb.net

Posted by Jonathan. on Stack Overflow See other posts from Stack Overflow or by Jonathan.
Published on 2010-01-08T16:30:24Z Indexed on 2012/10/22 17:01 UTC
Read the original article Hit count: 249

Filed under:
|
|
|

It seems the most obvious thing, but I just can't work out how to get the length of bytes sent over a network using a TCPClient and TCPListener? This is my code so far:

   'Must listen on correct port- must be same as port client wants to connect on.
    Const portNumber As Integer = 9999
    Dim tcpListener As New TcpListener(IPAddress.Parse("192.168.2.7"), portNumber)

    tcpListener.Start()
    Console.WriteLine("Waiting for connection...")

    'Accept the pending client connection and return 
    'a TcpClient initialized for communication. 
    Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
    Console.WriteLine("Connection accepted.")
    ' Get the stream
    Dim networkStream As NetworkStream = tcpClient.GetStream()
    '' Read the stream into a byte array

I need to get the length of the networkstream to set the size of the array of bytes I'm going to read the data into. But the networkStream.length is unsupported and does not work and throws an Notsupportedexception.

The only other way I can think of is to send the size of the data before sending the data, but this seems the long way round.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net