GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Feb-2006, 00:53
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Dynamic memory allocation of unknown/variable data length


hi all,
plz can any one help on how to dynamically allocate memory to hold unknown/variable data from a socket connection untill the client "i am the server" send the sequence <CRLF>.<CRLF>, as i am implememnting a minimal SMTP server using c++ and this is the part where the client send the email message body to the server "which is of course variable length" and the sequence <CRLF>.<CRLF> "\r\n.\r\n" indicates the end of the email message. so how can i allocate memory big enough to hold the message data ?
thanks alot
  #2  
Old 13-Feb-2006, 07:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Dynamic memory allocation of unknown/variable data length


Since you can't know how many bytes are being transferred, you have to guess. But that guess can be educated.

But first the answer to these questions can help:
1) If you read only a partial message (100 of 150 bytes sent) is the rest of the message lost?
2) Can all communications add a byte count to the beginning of each message since you are designing implementation?

Also, have you read the Guidelines? They will help you post questions that can be answered without us asking more questions just to understand your difficulty.
__________________

Age is unimportant -- except in cheese
  #3  
Old 13-Feb-2006, 07:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Dynamic memory allocation of unknown/variable data length


Quote:
Originally Posted by Radi0ShacK
hi all,
plz can any one help on how to dynamically allocate memory to hold unknown/variable data from a socket connection untill the client "i am the server" send the sequence <CRLF>.<CRLF>, as i am implememnting a minimal SMTP server using c++ and this is the part where the client send the email message body to the server "which is of course variable length" and the sequence <CRLF>.<CRLF> "\r\n.\r\n" indicates the end of the email message. so how can i allocate memory big enough to hold the message data ?
thanks alot

You access the data from the socket by calling a function like recv(), recvfrom() or read(), right? In addition to the argument for the socket file descriptor, these functions take an argument with the address of a buffer and an argument that tells the maximum number of bytes to attempt to read.

Create a buffer (array of char). Make it whatever size you think is appropriate. (Depends on system, memory, nature of message, ... whatever)

Call the function with the size of the buffer as the maximum number of bytes to be read. If the message contains more than the number of bytes in the buffer, then you allocate (using malloc(), realloc(), or some such thing) bytes to copy the buffer full of stuff, and call read() (or whatever) again. Repeat until the end of message has been detected.

Instead of having the entire message in memory, you could write a bufferfull at a time to a file for temporary storage and process the file (whatever you need to do with the message) after the entire message has been received.

For sanity's sake, the program might have some absolute maximum message length built in so that it wouldn't run away or get completely bogged down if the end-of-message indication was somehow missed.

If you are using some other method to get data from the socket, post some code; maybe someone can help.

Regards,

Dave
  #4  
Old 13-Feb-2006, 08:45
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: Dynamic memory allocation of unknown/variable data length


first thanks davekw7x and WaltP "and i will read it srry " and davekw7x
yes i will be using recv() as SMTP is connection oriented protocol. so according to what u told me, here are the steps that i am going to follow:
1-create a temporary buffer let's say char tempBuff[128];
2-call recv();
3-check whether the recieved data contains end of message sequence "\r\n.\r\n"
if not create more buffer either by malloc() or new() or realloc(), and call recv() again and repeat the above steps till the enf of message sequence is recieved
that i may write this data to a file.
thanks alot!
Quote:
Can all communications add a byte count to the beginning of each message since you are designing implementation?
i am just developing only the server not the client so i cant define my own SMTP protocol
thanks again.
  #5  
Old 13-Feb-2006, 09:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Dynamic memory allocation of unknown/variable data length


Quote:
Originally Posted by Radi0ShacK
i will be using recv() as SMTP is connection oriented protocol
.
.
.

Let us know how it turns out!

Regards,

Dave
  #6  
Old 14-Feb-2006, 01:43
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: Dynamic memory allocation of unknown/variable data length


Quote:
Let us know how it turns out!
of course whenever i finish it
  #7  
Old 14-Feb-2006, 05:02
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: Dynamic memory allocation of unknown/variable data length


hi i am facing a problem when i re-allocate more memory using realloc i lose the string i recieved before re-allocating, plz can any body help me?
CPP / C++ / C Code:
char *KpSMTP::getMsgBdy() 
{
	int lenRCV=0;
	int idx=0;
	char *tempBuff=NULL;
	bool eod=false; //EndOfData boolean variable
	tempBuff= (char *) malloc((sizeof(char)*128)); //allocate 128-bit memory
	if(tempBuff==NULL)
	{
		fprintf(stderr,"\ngetMsgBdy: ERROR malloc()\n");
		return NULL;
	}
	
	while(eod!=true)
	{
		lenRCV=recv(newfd, tempBuff, strlen(tempBuff), 0);
		tempBuff[lenRCV]='\0';
		//check for occuarence of end of data indicator "\r\n.\r\n"
		if(strstr(tempBuff, "\r\n.\r\n")!=NULL)
		{
			//remove eod indicator from buffer
			eod=true; //hoarry eod indicator is found
			puts("eof indicator is found!");
		}
		if((tempBuff= (char *) realloc(tempBuff,sizeof(char)*(128*2)))==NULL)
		{
			fprintf(stderr,"\ngetMsgBdy: ERROR realloc()\n");
			return NULL;
		}
	}
	puts(tempBuff);
	return tempBuff;
}
i just want to store all the message on the memory
thanks alot in advance
  #8  
Old 14-Feb-2006, 09:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Dynamic memory allocation of unknown/variable data length


Quote:
Originally Posted by Radi0ShacK
hi i am facing a problem when i re-allocate more memory


Suppose the value of an int variable "blocksize" is some arbitrary number, say, 512 (maybe bigger, maybe smaller, depending on what you think will be a typical message length.) For purposes of illustration, let's say it is 512.

Suppose the value of an int variable "length" will keep track of the total number of bytes that have been allocated.

Suppose your buffer is named "buf" (that is "buf" is a pointer to char).

Suppose that we have another pointer to char, say "readpoint", that will keep track of where in the buffer that the next block will be read.

Then, here's a generic method. You can tailor it for your exact requirements. Anyhow, here's how to use realloc() to expand the buffer a block at a time.


1. use malloc to get "blocksize" bytes; Set "length" to "blocksize". Set buf to the value returned by malloc(), Set the "readpointer" to buf. (First block will be read into the beginning of the buffer).
CPP / C++ / C Code:
  char *temp; /* will be used for realloc() */
  char *buf;
  char *readpointer;
  buf = malloc(blocksize);
  /* test returned value to see if it is NULL. If so, then malloc() failed. abort */
  readpointer = buf;
  length = blocksize;


2. Try to read "blocksize" bytes into the address that "readpointer" points to . If fewer than "blocksize" bytes are read, you are done. Go to step 4 to check for valid end of message, or whatever you are going to do with the message. If "blocksize" bytes were read, then to go step 3.

3. Use realloc() to get "length+blocksize" bytes, thereby extending the buffer
CPP / C++ / C Code:
  temp = realloc(length+blocksize);
  /* check to make sure that temp is not NULL 
   * If it is NULL, then realloc failed, and you free(buf)
   * and abort.
   * if temp is not equal to NULL, then do this:
   */
  buf = temp; /* this is the new beginning of the block */
  /* 
  * then
  *   add "blocksize" to the old read pointer,
  *   add "blocksize to "length"
  */

Now go back to step 2 to read the next block

4. Now process the message.


After all is done, use free() on buffer pointer. (The last value you got from realloc())


Note that sizeof(char) is always equal to 1 (it's in the C Standard). If you want to multiply the allocation number by sizeof(char) each time, it's not wrong; it's just redundant.

Note that there is no limitation, in general, on what you get from a socket (may have byte values equal to 0x00, for example). Therefore, you can't, in general, use strstr() or any other C standard library functions (strlen(), etc) on the contents of your buffer, since a zero byte will signify the end of the "string" being processed. I strongly recommend that you use loops to step through the bytes testing for whatever you want to find.

Note, in particular, that even if you believe that a "good" message never has a zero byte, sometimes you get stuff that is not good. You should never (and I mean it: never) assume that input is "good".

Regards,

Dave
  #9  
Old 14-Feb-2006, 09:56
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: Dynamic memory allocation of unknown/variable data length


davekw7x thanks alot again
  #10  
Old 14-Feb-2006, 10:05
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Dynamic memory allocation of unknown/variable data length


Quote:
Originally Posted by Radi0ShacK
hi i am facing a problem when i re-allocate more memory using realloc i lose the string i recieved before re-allocating, plz can any body help me?
Dave told you how you can do it, but he didn't address what your code did wrong. In a nutshell:
CPP / C++ / C Code:
lenRCV=recv(newfd, tempBuff, strlen(tempBuff), 0);
first time this line is executed it loads 128 characters into tempBuff. On the next execution of the statement, tempBuff is 128 bytes larger and you read the next 512 into tempBuff, starting at the beginning, overwriting what was there before. You need to
1) keep track of where the newly allocated portion of the buffer starts
2) read only 128 bytes into the new section of the buffer
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Memory de-allocation during debugging gaoanyu C Programming Language 12 19-Dec-2005 04:50
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.