I have established a connection with a Tomcat java server.
After sending login data, the server responds. The length of the response is not known.
So I'm trying to read byte after byte using DataReader.UnconsumedBufferLength
DataReader din = new DataReader(socket.InputStream);
int i = 0;
byte[] b = new byte[64];
await din.LoadAsync(1);
while(din.UnconsumedBufferLength > 0)
{
din.LoadAsync(1);
b[i] = din.ReadByte();
await din.LoadAsync(1)
i++;
}
This solution kind of works, I get the message into the byte array, but it is far from ideal. The corresponding Java client uses this little line of code
BufferedInputStream inFromServer = new BufferedInputStream(socket.getInputStream());
int read = 0;
byte[] result = new byte[100];
read = inFromServer.read(result);
I wish there was a equally simple solution in C#....
There is absolutely an easy equivalent in C# of the java code you posted. Actually, the C# version you are currently pursuing uses Asynchronous calls which is much more sophisticated than a simple call to your stream. If you want to use a buffered stream in C# without async calls, you can just Read
on the stream. As easy as:
socket.InputStream.Read()
In your java code it seems that you are using a buffer of size 100 to read the stream. You only read 100 bytes of the stream and not all the way to the end. If that is what you want you can do:
byte[] b = new byte[100];
int readTo = socket.InputStream.Read(b, 0, 100);
Also, if you want to go all the way to the end, use Jon Skeet's answer here. It doesn't get any better than that.