Lazarus

Programming => Networking and Web Programming => Topic started by: Trenatos on November 27, 2018, 04:32:25 am

Title: [Solved] TSocketStream.Write/WriteAnsiString -- Missing and extra chars
Post by: Trenatos on November 27, 2018, 04:32:25 am
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?
Title: Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
Post by: bytebites 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));
Title: Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
Post by: Thaddy 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.
Title: Re: TSocketStream.Write/WriteAnsiString -- Missing and extra chars
Post by: Trenatos 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
TinyPortal © 2005-2018