I'm attempting to retrieve a file via http, plain text works flawlessly, but with most binary files, after the header information, there is a NUL character, at which point, the psp stops receiving data, so in the case of a jpg, I only recieve the first 4 characters, what can I do to fix this?
int capacity = 256;
int len = 0;
u8* buffer = (u8*) malloc(capacity);
while (1) {
pspDebugScreenPrintf(".");
// read data
int count = recv(sock, (u8*) &buffer[len], capacity - len, 0);
// in blocking mode it has to return something, otherwise it is closed
// (which is the default for HTTP/1.0 connections)
if (count == 0) break;
if (count < 0) {
pspDebugScreenPrintf("read error: %i\n", errno);
break;
}
// adjust buffer, if needed
len += count;
if (len + 256 > capacity) {
capacity *= 2;
buffer = realloc(buffer, capacity);
if (!buffer) break;
}
}
Are you really sure you "only recieve the first 4 characters"?
Maybe you recieve the whole file (the actual lenght is in the "len" variable) but then you're checking the length with something like strlen(), and since strlen() looks for the first "NUL character" you get a lenght of 4.
ufoz wrote:Keep in mind that printf considers \0 as end-of-string.
What would you recommend as a way to verify for certain that I'm getting or not getting all the data?
Even though printf may stop at the NUL, I have the printf in the while {} which means it would be called repeated times, if data was still being added to buffer before the while breaks, printf would be called again and again, and it's not, it's only called one last time, right as the nul occurs.
If you have a suggestion though, to make 100% positive, I will CERTAINLY try it.
ufoz wrote:Keep in mind that printf considers \0 as end-of-string.
What would you recommend as a way to verify for certain that I'm getting or not getting all the data?
Even though printf may stop at the NUL, I have the printf in the while {} which means it would be called repeated times, if data was still being added to buffer before the while breaks, printf would be called again and again, and it's not, it's only called one last time, right as the nul occurs.
If you have a suggestion though, to make 100% positive, I will CERTAINLY try it.
Michael
Sorry. Just saw this thread. They are right in that printf() would stop with the null. Did you try doing a printf starting from the 6th byte of buffer to see what it shows you? If you memset that buffer to all nulls and then do the receipt of the data and if you are really only getting 4 bytes + the null then that 6th byte would be a null, rather than anything else.
It would help if you could post a copy of the code snippet you are using to try to printf the received data. That being said, there are two things you can try:
1. Instead of trying to dump the received data, just print out the total number of bytes received from the socket. This number should quickly rise to be the file size.
// Assumes 'buffer' is the array of received bytes
// and 'len' is the total number of bytes received
for (int n=0; n<len; n++)
printf("%02X ", buffer[n]);