Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
Can anyone tell me: "How To Construct A Header?" I am writing a FTP Client-Server Code. And I want to construct a Header for the data packets I have to send to server with information like the size of my data,etc.

Any pointers towards sample/example code would be appreciated.

[edit]
Thanks a lot. Would you please give me some idea on: Follow the code below:
C++
do
{
  dwRecv = recv(SockAccept, pHeader, sizeof(MSG_HEADER), NULL) ;
  if(dwRecv == SOCKET_ERROR)
  {
    printf("Main:: recv Failed: %d\n", WSAGetLastError()) ;
    break ;
  }
}while(dwRecv > 0) ;


Now if I allocate a buffer to read rest of the data, how can I set the buffer in recv in place of pHeader? And is there any solution for large data, so that I can do in recv-allocate-recv loop???
[/edit]
Posted
Updated 1-Mar-12 2:48am
v2

1 solution

Use a structure[^] and just add the fields you want into it. As long as both client and server use the same structure you should be able to do what you want. The general protocol of your message would then be to send the fixed length structure first, which will contain, at least, the length of the data message, followed by the data.
 
Share this answer
 
Comments
Abhineet Ayan Verma 1-Mar-12 7:47am    
Can you please give me some code example. Do I have to define two structs with same members at both Client-Server???
Richard MacCutchan 1-Mar-12 8:22am    
Just define your struct in a header file which is included into both projects. Try and use as much common code as possible to ensure compatibility at both ends.
Abhineet Ayan Verma 1-Mar-12 8:43am    
Thanks a lot. Would you please give me some idea on: Follow the code below:
<pre lang="c++">
do
{
dwRecv = recv(SockAccept, pHeader, sizeof(MSG_HEADER), NULL) ;
if(dwRecv == SOCKET_ERROR)
{
printf("Main:: recv Failed: %d\n", WSAGetLastError()) ;
break ;
}
}while(dwRecv > 0) ;</pre>

Now if I allocate a buffer to read rest of the data, how can I set the buffer in recv in place of pHeader? And is there any solution for large data, so that I can do in recv-allocate-recv loop???
Richard MacCutchan 1-Mar-12 8:50am    
Extract the relevant values from the structure, allocate a data buffer and create a new recv() loop to read the message.
Abhineet Ayan Verma 1-Mar-12 9:31am    
But what if the data is 500mb or more. I cant allocate that much, that would not be good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900