Odczyt strumienia, TcpClient

0

witam
mam taka metode, wszystko dziala ok, ale przy ostatnim obiegu petli, gdy juz zostaje do odczytu kilka bajtow, kod zamarza na jakis czas na

while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)

. Po jakims czasie wychodzi z petli i wszystko jest ok, ale nie moge tyle czekac, szczegolnie ze caly czas dziala, az do 'ostatniego' odczytu.

public byte[] ReadFully(NetworkStream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
                initialLength = 32768;

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                        return buffer;

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

ktos cos poradzi?

0

pozwolę sobie podbić topic.

0

Problem chyba leży po stronie serwera nie klienta. Próbowałem użyć Twojej funkcji dla prostego klienta, który czyta z NetworkStream i opóźnienie było tylko w sytuacji, gdy po stronie serwera poczekałem z ostatnim Flushem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1