You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since I don't know the length of the response the server sends to me I create a buffer that I'm sure is large enough for it (1024 bytes).
Using networkStream.Read(data, 0, data.Length); to then read into that stream gives me a CA2022 warning: Avoid inexact read with 'System.Net.Sockets.NetworkStream.Read(byte[], int, int)', suggesting I should use NetworkStream.ReadExactly instead.
This will block execution forever since the server never sends data that's 1024 bytes long to me.
My question is what the correct way is to do this without receiving this warning.
Thanks!
The text was updated successfully, but these errors were encountered:
You can check the return value of networkStream.Read(data, 0, data.Length) to see how much data you received. This is also suggested in the doc of CA2022 on how to fix a violation:
To fix a violation, either check the return value (which is the total number of bytes read into the buffer) or ...
The fixer of CA2022 assumed that you always wanted to read exactly 1024 bytes (since you did not check the return value). Here is another document that outlines recommended actions when working with Stream.Read and Stream.ReadAsync.
Using this code snippet, I'm connecting to a TCP server, write a short message to it and then read it's response.
Since I don't know the length of the response the server sends to me I create a buffer that I'm sure is large enough for it (1024 bytes).
Using
networkStream.Read(data, 0, data.Length);
to then read into that stream gives me aCA2022
warning:Avoid inexact read with 'System.Net.Sockets.NetworkStream.Read(byte[], int, int)'
, suggesting I should useNetworkStream.ReadExactly
instead.This will block execution forever since the server never sends data that's 1024 bytes long to me.
My question is what the correct way is to do this without receiving this warning.
Thanks!
The text was updated successfully, but these errors were encountered: