views:

678

answers:

1

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.

A: 

Not sure what object type you are expecting but this works and you could keep track of the final size, sorry in C# though:

System.Net.Sockets.NetworkStream nwStream = objTcp.GetStream();
System.IO.StreamReader stReader = new System.IO.StreamReader(nwStream);

string strTemp = ReadFromBuffer(stReader);

private string ReadFromBuffer(System.IO.StreamReader objReader)
{
        int intRead=1024;
        System.Text.StringBuilder stbBugger = new StringBuilder();
        char[] chrTempBuffer = new char[1024];

        while (intRead == 1024)
        {
            intRead = objReader.Read(chrTempBuffer, 0, 1024);
            stbBugger.Append(chrTempBuffer, 0, intRead);
        }

        return stbBugger.ToString();
    }

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.

Joshua Cauble
That will only read 1024 bytes max though?I need to the size of the data `before` getting the data so i can set how many bytes to read.
Jonathan
It actually reads a max of 1024 at a time. If there is less it will read less. Hence the while loop. The read command will actually return the number of bytes read. However you need to set a buffer first.
Joshua Cauble
Yes but what if its more than 1024 bytes??And the reason I need the length of data is to set the buffer!!!
Jonathan
I read more than 1024 bytes with this function all the time. Hence the while loop. So, the buffer is set to 1024, so you read 1K chunks off the stream at a time into your stringbuilder. Each read reads the next 1K (or less if there is less) on the stream. Once you run out of data on the stream the while loop finishes. all your data is in string builder and your done.
Joshua Cauble
Thanks I got it now :)
Jonathan