Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using D2009 or later, then when sending, you're cutting the data in half. Also, given you will ultimately be reading the data with <code>ReceiveBuf</code>, it would probably be sensible to prepend a length marker. Less substantively, you also don't need to set the memory stream's <code>Size</code> up front, and should wrap the stream usage in a try/finally block:</p> <pre><code>procedure TForm1.Button1Click(Sender: TObject); var myStream : TMemoryStream; chat : String; Len: Int32; begin //Creating a stream chat := 'bladibla'; myStream := TMemoryStream.Create(); try Len := Length(Chat); myStream.WriteBuffer(Len, SizeOf(Len)); myStream.WriteBuffer(Pointer(Chat)^, Len * SizeOf(Char)); //Resetting the stream position myStream.Position := 0; //Sending the stream TcpClient1.Active := true; TcpClient1.SendStream(myStream); TcpClient1.Active := false; finally //Free the stream myStream.Free; end; end; </code></pre> <p>In the case of the receiving code, you haven't pre-allocated the <code>Chat</code> buffer. As such, <code>Length(Chat)</code> will be 0. Given my suggested code above, we can read off the length marker first:</p> <pre><code>procedure TForm1.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient); var chat : string; Len: Int32; begin //Receives the message from the client ClientSocket.ReceiveBuf(Len, SizeOf(Len),0); SetLength(Chat, Len); ClientSocket.ReceiveBuf(Pointer(Chat)^,Len * SizeOf(Char),0); memo1.Lines.Add(chat); memo1.Lines.Add('------'); end; </code></pre> <p>Lastly... the components used in this code are deprecated, so it might be an idea to investigate Indy instead.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload