Click here to Skip to main content
15,886,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a receiver buffer in which i am receiving data from the recv(); call i want to clear the buffer after receiving data from the recv(); call in to the buffer.


C++
#define DEFAULT_BUFLEN 512
char recvbuf[DEFAULT_BUFLEN];

int recvbuflen = DEFAULT_BUFLEN;
char common[276];
int main()
{

//After connect and send call...
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
            if ( iResult>0 )
              {
for(int j=0;j<=iResult;j++)
common[j]=recvbuf[j];

//NOW I WANT TO CLEAR recvbuf.......
//fflush can't be used,how can i do so?

}


Please tell by pseudo code
Posted

Why do you want to clear the buffer? Will it contain sensitive information?

I suggest you look up the memcpy() and memset() functions.

Also, your "recvbuf" is longer than "common", so copying from recvbuf to common could overflow, which is Very Bad.
 
Share this answer
 
Comments
Tarun Batra 17-Sep-12 5:28am    
Sir i want to copy the data of recvbuf into the common buffer,actually as i am making the socket connection,according to my documentation i should receive 138 bytes but i am receiving 276 bytes that is double of 138 bytes.
To avoid extra 138 bytes i am using the following code
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{
int prev=0;
//prev= iResult;
if(prev!=iResult)
{

for(j=0;j<=iResult;j++);
common[j] = recvbuf[j];

}
prev= iResult;
}
Tarun Batra 17-Sep-12 11:21am    
please reply this http://www.codeproject.com/Questions/460820/Can-u-give-me-an-idea-how-to-implement-this
You probably don't need to clear such buffer, anyway:
memset(recvbuf, 0, sizeof(recvbuf));

will do the job.



Tarun Batra wrote:
C++
for(int j=0;j<=iResult;j++)
common[j]=recvbuf[j];

As already noted by Graham Breach you are going to overrun the common buffer with such code.
 
Share this answer
 
Comments
Tarun Batra 17-Sep-12 5:26am    
Sir i want to copy the data of recvbuf into the common buffer,actually as i am making the socket connection,according to my documentation i should receive 138 bytes but i am receiving 276 bytes that is double of 138 bytes.
Tarun Batra 17-Sep-12 5:26am    
my code is something like this:
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{
int prev=0;
//prev= iResult;
if(prev!=iResult)
{

for(j=0;j<=iResult;j++);
common[j] = recvbuf[j];

}
prev= iResult;
}
CPallini 17-Sep-12 5:34am    
If you need 138 bytes then why don't you ask for exactly 138 bytes?
Tarun Batra 17-Sep-12 5:40am    
sir i send one request command to a machine and receive the response from the machine,the response should be 138 bytes but i am 276 bytes i get a response like this:-
Bytes received: 14
Bytes received: 14
Bytes received: 16
Bytes received: 16
Bytes received: 18
Bytes received: 18
Bytes received: 17
Bytes received: 17
Bytes received: 14
Bytes received: 14
Bytes received: 15
Bytes received: 15
Bytes received: 19
Bytes received: 19
Bytes received: 15
Bytes received: 15
Bytes received: 10
Bytes received: 10
this is the output of following statements:-
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{ printf("Bytes received: %d\n", iResult);
}
CPallini 17-Sep-12 5:50am    
I suppose you should post ALL the relevant code to get better help.
You want to clear all contents of buffer? If that is right you can use a for loop to set elements of it to zero.
 
Share this answer
 
Comments
Tarun Batra 17-Sep-12 11:22am    
please reply this http://www.codeproject.com/Questions/460820/Can-u-give-me-an-idea-how-to-implement-this

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