![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Dynamic memory allocation of unknown/variable data lengthhi 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
|
||||
|
||||
Re: Dynamic memory allocation of unknown/variable data lengthSince 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
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthQuote:
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
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthfirst thanks davekw7x and WaltP "and i will read it srry
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:
thanks again. |
|
#5
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthQuote:
Let us know how it turns out! Regards, Dave |
|
#6
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthQuote:
|
|
#7
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthhi 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:
thanks alot in advance |
|
#8
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthQuote:
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:
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:
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
|
|||
|
|||
Re: Dynamic memory allocation of unknown/variable data lengthdavekw7x thanks alot again
![]() |
|
#10
|
||||
|
||||
Re: Dynamic memory allocation of unknown/variable data lengthQuote:
CPP / C++ / C Code:
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 GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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