Problem when sending a file with TCP lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- stab · 2013-09-17 05:47 Hi all, Could someone please explain what I'm doing wrong when trying to send a small png-image(579 bytes) with lNet-tcpsockets on same pc(Win 7/64 bits). The server sends the file in one go but In both client and server are declared: Code: [Select]  private     { private declarations }     FServerReceivedStr : string;     FImgStream : TMemoryStream;     FBuffer : array of byte;     FSize : integer;     FPos : integer; and FImgStream is created in FormOnCreate; From the client is sent a request for the file: Code: [Select]procedure TfrmlNetClient.btnGetImageClick(Sender: TObject); begin   FSize := 0;   SetLength(FBuffer, 10000);   Memo1.Lines.Add('Client1 requesting image');   FRequestingImage := true;   LTCPClient1.SendMessage('send image'); end; and in the server the request is received: Code: [Select]procedure TfrmlNetServer.LTCPServerReceive(aSocket: TLSocket); var   n : integer; begin   if aSocket.GetMessage(FServerReceivedStr) > 0 then   begin     if FServerReceivedStr <> 'send image' then       Memo1.Lines.Add('Server received: ' + FServerReceivedStr)     else     begin       FImgStream.LoadFromFile(ExtractFilePath(Application.Exename) + 'test.png');       n := FImgStream.Size;                      n is 579 as it should be       SetLength(FBuffer, FImgStream.Size);       n := Length(FBuffer);                      n is 579 as it should be       FImgStream.Position := 0;       FImgStream.Read(FBuffer, FImgStream.Size);       n := Length(FBuffer);                      !!! SIGSEV !!! How come???       FPos := 0; // send image       LTCPServerCanSend(aSocket);     end;   end; end; procedure TfrmlNetServer.LTCPServerCanSend(aSocket: TLSocket); var   n : integer; // will store results of .Send() begin   if FServerReceivedStr = 'send image' then   begin   { repeat the sending until we sent the whole buffer, or .Send return 0 -- means we filled the send buffer for now. NOTE: FSendIndex  has to begin at 1 for a string! }     repeat     { try to send the rest of the buffer we didn't send yet }       n := Length(FBuffer) - FPos;               !!! SIGSEV again!!!       if n > 0 then         n := LTCPServer.Send(FBuffer[FPos], n);     { if we sent something, move us in the buffer so we don't re-send the same part }       Inc(FPos, n);     until (n = 0) or (FPos > Length(FBuffer));   end   else     LTCPServer.SendMessage(Edit1.Text, aSocket); end; and eventually the image is received in the client: Code: [Select]procedure TfrmlNetClient.LTCPClient1Receive(aSocket: TLSocket); var   s : string;   n, t : integer; begin   if not FRequestingImage then   begin     if aSocket.GetMessage(s) > 0 then       Memo1.Lines.Add('Client1 received: ' + s);   end   else   begin     n := aSocket.Get(FBuffer, 100);     FSize := FSize + n;     if n > 0 then     begin       FImgStream.Write(FBuffer, n);       Exit;     end;     FRequestingImage := false;     n := FImgStream.Size;     FImgStream.Position := 0;     Image1.Picture.Bitmap.LoadFromStream(FImgStream);     FBuffer := nil;   end; end; My questions: Why do I get a SIGSEV in LTCPServerReceive? Am I supposed to first send number of bytes in the file to client for it to know how many bytes should be read? Is one, on the receiving side, also forced to read block of data because of input buffer overflow? Very thankful for guidance Regards stab ---------------------------------------------------------------------- ludob · 2013-09-17 06:19 QuoteCode: [Select]FImgStream.Read(FBuffer, FImgStream.Size);You are overwriting the pointer to the buffer, FBuffer, instead of writing in the buffer. Use Code: [Select]FImgStream.Read(FBuffer[0], FImgStream.Size); ---------------------------------------------------------------------- stab · 2013-09-17 10:34 Thanks alot, I became blind Regards stab