Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio
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.
 

#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 16-Sep-12 22:32pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Tarun Batra at 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 at 17-Sep-12 11:21am
   
please reply this http://www.codeproject.com/Questions/460820/Can-u-give-me-an-idea-how-to-implement-this
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You probably don't need to clear such buffer, anyway:
memset(recvbuf, 0, sizeof(recvbuf));
will do the job.
 

 
Tarun Batra wrote:
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.
  Permalink  
Comments
Tarun Batra at 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 at 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 at 17-Sep-12 5:34am
   
If you need 138 bytes then why don't you ask for exactly 138 bytes?
Tarun Batra at 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 at 17-Sep-12 5:50am
   
I suppose you should post ALL the relevant code to get better help.
Tarun Batra at 17-Sep-12 6:00am
   

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "100"
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
SOCKET ThreadSocket = INVALID_SOCKET;
static DWORD WINAPI FIComThreadEntry(LPVOID lpdwThreadParam);
HANDLE gui_ThreadHandle[4];
bool connect(char *host,int port, int timeout);
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
 
char recvbuf[DEFAULT_BUFLEN];
 
int recvbuflen = DEFAULT_BUFLEN;
void ReadConfigSettings();
void GetIniFilePath();
void CreateLogFolder();
//time_t t;
 
int m_port;
char *port2;
char *ip1="192.168.15.168";
char *ip_serv;
char data[6]="CM00";
const char data1[]={0x43,0x4d,0x30,0x30,0x0f,0x0D};
const char data2[]={0x43,0x4d,0x30,0x30,0xf0,0x0D};//f0
SYSTEMTIME st,lt;
 
Log* wLog;
char *b;
int LOGLVL;
const char* szLogDir;
char IniFilePath[2500];
void datarecv();
#define Buflen 1400
char common[276];
//char Buf[MaxSize][Buflen];
int __cdecl main(int argc, char **argv)
{



/*time_t time;
time(&time);*/
 
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
 

u_long iMode = 0;
DWORD nTimeout = 5000; // 5 seconds
// Validate the parameters
if (argc != 2) {
printf("usage: %s server-name\n", argv[0]);
return 1;
}
 
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
 
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
 
// Resolve the server address and port
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
 
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
 
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
/////////////////////////////////////////////////////////////////////////////
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.
 
iResult = ioctlsocket(ConnectSocket, FIONBIO, &iMode);
if (iResult != NO_ERROR)
printf("ioctlsocket failed with error: %ld\n", iResult);
/////////////////////////////////////////////////////////////////////////////
 

iResult = setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
if (iResult != NO_ERROR)
{
printf("\nSetsocopt fail with error :%d\n",WSAGetLastError());
 
return 0;
}
 

// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
 
freeaddrinfo(result);
 
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
 
DWORD FIComThreadId;
 
gui_ThreadHandle[1] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)FIComThreadEntry,(LPVOID)ConnectSocket, 0, &FIComThreadId);
 
if(gui_ThreadHandle[1] == NULL)
{
printf("Error while creating FIComThreadEntry thread.\n");
printf("Leaving from FIComThreadEntry\n");
 
return 0;
}
while(1);
 
}
 
static DWORD WINAPI FIComThreadEntry(LPVOID lpdwThreadParam)
{
ThreadSocket=(SOCKET)lpdwThreadParam;
data[4]=0x0F;
//data[4]=0xF0;
data[5]=0x0D;
int i=0;
int j;
//while(1)
{
 
iResult = send( ThreadSocket, data, (int)strlen(data), 0 );
if (iResult == SOCKET_ERROR)
{
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
 
printf("Bytes Sent: %ld\n", iResult);
int prev
Tarun Batra at 17-Sep-12 6:04am
   
int w =0;
do {//WORKING
 
//iResult = recvfrom(ThreadSocket, recvbuf, recvbuflen, 0,ptr->ai_addr, (int *)ptr->ai_addrlen);
iResult = recv(ThreadSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
{

printf("Bytes received: %d\n", iResult);
}
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError()); } while( iResult > 0 ); return 0;
}
Tarun Batra at 17-Sep-12 6:05am
   
its int prev=0; and then code followed by int w=0;
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Tarun Batra at 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)

  Print Answers RSS
0 OriginalGriff 6,696
1 Sergey Alexandrovich Kryukov 6,675
2 CPallini 5,315
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100