tags:

views:

62

answers:

2

Hi, I have this code:

ofstream dest("test.txt",ios::binary);

 while (true){
  size_t retval = recv (sd, buffer, sizeof(buffer), 0);

   dest.write(buffer,retval);

 if(retval <= 0) { delete[] buffer; break;}
}

Now, the recv() function return 4 bytes each loop right? and buffer contain it, this return all data so, pseudo-header and binary data (image), but I want know how capture only binary data, I know that the end of header are "\n\r" right? but what's are the solution better for make this? I make a function that detect when are "\n\r"? and after how capture binary data? Or, I put all data in memory, and after parse it? but how? I'm desperate :(

+1  A: 

recv will return however many bytes it can up to the maximum you've told it, which is sizeof(buffer). If that is 4, then that's what will probably be read most of the time.

The "end of header" and such makes no sense since you haven't provided the context necessary to interpret it. I'm guessing it has something to do with packet headers but each protocol is different so we have no way of knowing.

Noah Roberts
A: 

To convert individual characters to binary try this:

ofstream dest("test.txt",ios::binary);

while (true){

    size_t retval = recv (sd, buffer, sizeof(buffer), 0);

    //Convert individual chars to binary
    // and output to file
    for(int i=0; i < retval; ++i) {
        dest << bitset<8>(buffer[i]);
    }        

    if(retval <= 0) { delete[] buffer; break;}
}

Make sure to add #include < bitset > to the file

Mike Webb
I don't want use an external library, and this method not divide a header from binary data, but convert all data to binary, right?
fixo2020
Well, everything that's in your buffer each time you receive. You can have a check to see if you reached the end of the header and exit the loop, but if you don't want to use any external libraries then this won't help you anyway.
Mike Webb
You can probably do it mathematically and manually by converting each character in the buffer to an int ( i.e. 'int temp = (int)buffer[i];' ) and then convert that to binary using the standard decimal-to-binary conversion technique. Since all chars have an ascii or unicode index then this can be done.
Mike Webb