title: Send/Receive text with buffer using sockets in Delphi
body: <p>So I have searched for a way to make this work but found no working solution. I am trying to send text that has been inserted in to a memo from the Client to the Server using sockets. Unfortunately I cannot get the coding working. It might be something stupid I'm doing wrong; but I don't know where the mistake is. I created a stable connection between the server and client, and I am able to send text using <code>Server.Socket.Connections[Form1.ListView1.Selected.Index].SendText('Texthere');</code> but I just cant get it to send and receive text or anything using buffers.</p>
  
  <p>Code on Client:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>procedure TIBAT.Sends1Click(Sender: TObject); //Button to start the sending of the text on the memo component
  var
    ms: TMemoryStream;
    size: Integer;
  begin
    if (Form1.ListView1.Selected &lt;&gt; nil) then //if a server is selected
    begin
    Form1.Server.Socket.Connections[Form1.ListView1.Selected.Index].SendText('IBAT');
      ms:= TMemoryStream.Create;
      try
        IBAT.Memo1.Lines.SaveToStream(ms);
        ms.Position:= 0;
        Size:= MS.Size;    
       Form1.Server.Socket.Connections[Form1.ListView1.Selected.Index].SendBuf(Size,SizeOf(Size));
       Form1.Server.Socket.Connections[Form1.ListView1.Selected.Index].SendStream(ms);
      except
        ms.Free;
        ShowMessage('FAILED');
      end;
    end;
  end;
  </code></pre>
  
  <p>Code on the Server:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>private
      Stream: TMemoryStream;
      FSize: Integer;
      writing: Boolean;
  
  ...
  
  procedure TSock.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
  var
    Command     :String;
    BytesReceived: Longint;
    CopyBuffer: Pointer; //buffer for copying
    ChunkSize: Integer;
    TempSize: Integer;
  const
    MaxChunkSize: Longint = 8192;
  begin
    Command := Socket.ReceiveText;
  if split(Command, '|', 0) = 'IBAT' then
    begin
    If FSize=0 then
        begin
          //ShowMessage(IntToStr(Socket.ReceiveLength)); added messageboxes everywhere to figure out where the problem is
          If Socket.ReceiveLength&gt;SizeOf(TempSize) then
          begin
            Socket.ReceiveBuf(TempSize,SizeOf(TempSize));
            Stream:= TMemoryStream.Create;
            Stream.SetSize(TempSize);
            //ShowMessage(IntToStr(TempSize));
            FSize:= TempSize;
            writing:= True;
          End;
        End;
        If (FSize&gt;0) and (writing) then
        begin
          GetMem(CopyBuffer, MaxChunkSize); //allocate the buffer
          While Socket.ReceiveLength&gt;0 do
          Begin
            ChunkSize:= Socket.ReceiveLength;
            If ChunkSize &gt; MaxChunkSize then ChunkSize:= MaxChunkSize;
            BytesReceived:= Socket.ReceiveBuf(CopyBuffer^,ChunkSize);
            Stream.Write(CopyBuffer^, BytesReceived); //write chunk
            Dec(FSize,BytesReceived);
          End;
          FreeMem(CopyBuffer, MaxChunkSize); //free allocated buffer
          If FSize = 0 then
          begin
          Stream.SaveToFile(TempDir+IntToStr(GetTickCount)+'.cmd');
          Socket.SendText('File received!');
          Stream.SetSize(0);
          FSize:= 0;
          End;
      FreeMem(CopyBuffer, MaxChunkSize);
      Writing:= False;
      End;
  end;
  </code></pre>
  
  <p>I know using Indy is more effective and better than sockets, but I've already started coding using it and switching over to Indy is a big pain. 
  If anyone can help me get this working I would really appreciate it.</p>