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.
Not sure what object type you are expecting but this works and you could keep track of the final size, sorry in C# though:
It all depends on what you are needing to read and what type of stream you use. The method here you could add a count to as well while reading the results if you really needed to.
Just keep track of the number of char’s read during the operation and you have a size plus the data.