Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:57:22+00:00 2026-06-09T09:57:22+00:00

I use winsock to receive a 1024byte buffer like this: var buffer : array[0..1023]

  • 0

I use winsock to receive a 1024byte buffer like this:

var
 buffer    : array[0..1023] of byte; 
 endarray  : array of byte;
 hFile     : THandle;
 dwWritten : DWORD;
 dwRead    : DWORD;
begin
 SetLength (endarray, 3000); //fixxed size (doesn't really matter here, cause I know the size)
 hFile := CreateFileW('test.bin', GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0);
 SetFilePointer(hFile, 0, nil, FILE_BEGIN);
 repeat
 dwRead := recv(MySock, Buffer, 1024, 0);
 WriteFile(hFile, buffer[0], dwRead, dwWritten, nil); // works fine!
 // Add the buffer to the endarray but how?
 until (dwRead = 0) or (dwRead = -1)
 [...]
 CloseHandle (hFile);
end;

How can I add the buffer to the endarray automatically so it actually gets appended to the end?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:57:23+00:00Added an answer on June 9, 2026 at 9:57 am

    Like this:

    var
      PrevLen: Integer;
    ....
    dwRead := recv(MySock, Buffer, 1024, 0);
    if dwRead>0 then
    begin
      PrevLen := Length(endarray);
      SetLength(endarray, PrevLen+dwRead);
      Move(Buffer[0], endarray[PrevLen], dwRead);
    end;
    

    And also remove the first line of code from your function that pre-allocates endarray.

    If you’d rather allocate the buffer once (as per the code in the question) then you can code it like this:

    var
      endarrayLen: Integer;
    .....
    endarrayLen := 0;
    repeat
      dwRead := recv(MySock, Buffer, 1024, 0);
      if dwRead>0 then
      begin
        Move(Buffer[0], endarray[endarrayLen], dwRead);
        inc(endarrayLen, dwRead);
      end;
      .....
    until ...
    

    But that’s a buffer overrun waiting to happen!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a simple project which use Winsock Tool. When I receive any
i would like to use winsock functions (and not QTcpSocket or QUdpSocket) in my
[I've followed the suggestion to use Winsock.pas, but it still writes garbage -- although
I currently use a simple winsock solution (WSAStartup, socket, connect, send, recv, closesocket) for
In a commercial Delphi library, WinSock API functions like WinSock.connect and WinSock.send are used
I have two programs written in C++ that use Winsock. They both accept TCP
I'm not looking to use something big like QT or wxWidgets' APIs. I just
Is it possible to use winsock on an AIX system? If not, is there
I got a struct for client and server like this: { char type; int
Is there any benefit on Windows to use the WSA winsock functions compared to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.