Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm communicating with a machine through LAN.
After setting up the the TcpClient and sending the NetworkStream.write command with the machine command to return data to me. The stream successful sends all the packets with its Closing flags which are (0xAA and 0x55) though the stream.read gets stuck in the While loop statement and never exits it. Through wireshark I can see that it sent all the packets and after the machine keeps the stream alive every couple minutes in wireshark.

What I have tried
- Exit the loop when it see's the closing flag (0xAA and 0x55)
- The machine sends back 3 packets that contain the size of the entire packet and closing after that has been met

Have had no luck in exiting this while statement after trying several methods.
Any ideas. Note that machine never sends -1 which I believe is 0xFFFFFF it sends 0xAA and 0x55 in 2 bytes as the closing flags but I cant get out of this Loop

C#
if (initialLength < 1)
           {
               initialLength = 32768;
           }

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

           int chunk;
           int buffSize = 500000;
           while ((chunk = stream.Read(buffer, read, buffer.Length - read)) < buffSize)
           {
               read += chunk;

               // If we've reached the end of our buffer, check to see if there's
               // any more information
               int interval = 0;
               if (interval == 0)
               {
                   byte[] aSize = new byte[3];
                   aSize[0] = buffer[2];
                   aSize[1] = buffer[3];
                   aSize[2] = buffer[4];


                   buffSize = ConvertByteArrayToInt(aSize);

               }

               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;
Posted
Comments
Richard MacCutchan 24-Oct-11 11:49am    
You could if you checked your return values properly.

Obvious question: do you ever close the connection at the other end?

NetworkStream.Read will return 0 if and only if the connection has been terminated (otherwise it will block until there is at least one byte). So check for that in your loop condition. Read will also never read more than the maximum size you pass it as a parameter, so the check you're doing right now is pointless. Try
while(0 < stream.Read( ... )) { ... }
 
Share this answer
 
Comments
roman_s 24-Oct-11 14:10pm    
I tried the above code, the application still hangs and wireshark reports end of stream (after several expected packets, I get 0xAA, 0x55 as my closing flags) then the system continues to keep the connection alive. I can't exit the loop code to close the connection and need to keep the while statement in receiving.

Through this code that I wrote, I can get the size of bytes to expect.
I also can expect 0xAA followed by 0x55 as the closing flag.

Any ideas how I can implement this to get out of the while loop and close connection?
int interval = 0;
if (interval == 0)
{
byte[] aSize = new byte[3];
aSize[0] = buffer[2];
aSize[1] = buffer[3];
aSize[2] = buffer[4];

buffSize = ConvertByteArrayToInt(aSize);

}
BobJanova 24-Oct-11 16:18pm    
One end of the connection needs to actively close it. Testing for Read() = 0 will escape the while loop if the other end closes it (i.e. calls connection.Close) – you didn't answer whether that was the case ;).

This might be the problem:
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();

That ReadByte could hang forever (well, until the connection times out) if it really is the end of the content (as the comment indicates). Use Stream.Available to check if there is something more without waiting.

Sergey Alexandrovich Kryukov 24-Oct-11 19:14pm    
Good point, my 5.
--SA
Hmm. Maybe not a solution, but doesn't the Read method block while waiting for more data then return 0 at the end of stream? I haven't looked too closely but could see the while loop thrashing at the end of the stream.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900