How to use HttpListener to receive byte [] data sent by the client

834 views Asked by At

I am very sorry that my c # foundation is limited, but this question has troubled me for many days. I hope the seniors will give me some advice. Thank you very much。 My client sends the message this way:

public IEnumerator SetAnchorData(string Url, byte[] data)
{ 
    UnityWebRequest request = UnityWebRequest.Put(Url, data);
    yield return request.Send();
    if (request.isError)
    {
        Debug.Log(request.error);
    }
    else
    {
         text.text += "succeed"+data.Length;
        // Debug.Log("succeed" + data.Length);
    }
    request.Dispose();
}

My server reads the data like this:Get the data flow here

HttpListenerRequest request = context.Request; 
StreamReader reader = new StreamReader(request.InputStream);

Then you pass the "reader" down to this method to get byte []

 `private byte[] ReadLineAsBytes(StreamReader SourceStream)
    {
        var resultStream = new MemoryStream();
        while (!SourceStream.EndOfStream)
         {
            resultStream.WriteByte((byte)SourceStream.Read());
            NUM++;
         }
        Console.Write("It's been read {0} times:", NUM);

        resultStream.Position = 0;
        byte[] dataBytes = new byte[resultStream.Length];
        resultStream.Read(dataBytes, 0, dataBytes.Length);
        return dataBytes;
    }`

The result is that the Length of byte [] is less than the Length of the client byte []. I don't know where the problem is. If it is the reading method, please teach me how to do it. Please help me, please, please, please.

0

There are 0 answers