Hello @thebluetropics ,
Welcome to Microsoft Q&A!
TransmitFile
in win32 api seems to be the api that meets your needs, but using this api will limit your transmission. For details, please refer to the documentation
https://learn.microsoft.com/zh-cn/windows/win32/api/mswsock/nf-mswsock-transmitfile#remarks
#pragma pack(push,1) //Start to define the data pack, using byte alignment
/*---------------------- Package head----------------------*/
typedef struct tagPACKAGEHEAD
{
BYTE Version;
WORD Command;
WORD nDataLen;//Length of packet body
}PACKAGE_HEAD;
#pragma pack(pop) //End defining the data pack, restore the original alignment
If TransmitFile does not meet your needs. You can refer to the following processing logic to see if it meets your needs.
- Dynamically allocate a buffer for each connection, and associate this buffer with SOCKET, which is commonly associated with a structure.
- When receiving data, first store this segment of data in the buffer.
- Judge whether the length of the data in the buffer area is enough for the length of a packet header, if not, the unpacking operation is not performed.
- Parse out the variable representing the length of the packet body according to the packet header data.
- Determine whether the length of the data in the buffer area except the packet header is enough for the length of a packet body. If it is not enough, the unpacking operation is not performed.
- Take out the entire data packet and delete it in the buffer. Move the data behind the packet to the start address of the buffer.
Thank you.
Junjie