Recent

Author Topic: [Solved] TSocketStream.Write/WriteAnsiString -- Missing and extra chars  (Read 268 times)

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
I'm going insane.

I'm trying to write to a TSocketStream, a response to a webbrowser request (Testing with curl)

I can't figure out how to send what I want to send..

If I use WriteAnsiString(res) I get an extra random character in the beginning

Code: Pascal  [Select]
  1. res: AnsiString;
  2.  
  3. res := 'HTTP/1.1 200 OK';
  4.  
  5. Data.WriteAnsiString(res);
  6.  

And if I use Data.Write(tmp, 15) it eats the first character.

Code: Pascal  [Select]
  1. tmp: Array[0..14] of Char;
  2. res: AnsiString;
  3.  
  4. res := 'HTTP/1.1 200 OK';
  5. tmp := res;
  6.  
  7. Data.Write(tmp[1], 15);
  8.  

What am I missing?
« Last Edit: November 27, 2018, 01:43:36 pm by Trenatos »

bytebites

  • Full Member
  • ***
  • Posts: 164
Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
« Reply #1 on: November 27, 2018, 05:14:44 am »
WriteAnsiString writes length and the string itself.
Maybe you need
Code: Pascal  [Select]
  1. Data.Write(res[1],length(res));

Thaddy

  • Hero Member
  • *****
  • Posts: 7182
Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
« Reply #2 on: November 27, 2018, 10:00:17 am »
Indeed!
And the character is not random......

( You can also cast to a PChar)
A stream is a stream. EVERY byte is important.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

Trenatos

  • Sr. Member
  • ****
  • Posts: 454
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
« Reply #3 on: November 27, 2018, 01:43:15 pm »
I wasn't going insane, but I did have a copy pasta problem..

I grabbed a reply string from a C example, including \n, I switched to sLineBreak and with WriteAnsiString() the browser now shows data with no error.

So I'm guessing that with Write() I have to add the first byte manually, it "disappeared" because it was treated as a size marker.

WriteAnsiString() worked fine, so I should have just used linebreaks properly, and the "extra" character is the size marker but it shows up as an ascii character with the CURL output.

On to the next challenge!  :)

Thanks guys