 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
planetusa
Joined: 06 Dec 2005 Posts: 21 Location: Asheville
|
Posted: Tue Jan 10, 2006 5:20 pm Post subject: Network Connection drops upon receipt of NUL character |
|
|
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?
| Code: | 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;
}
} |
Thanks,
Michael |
|
| Back to top |
|
 |
Saotome

Joined: 03 Apr 2004 Posts: 182
|
Posted: Wed Jan 11, 2006 8:30 am Post subject: |
|
|
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. _________________ infj |
|
| Back to top |
|
 |
planetusa
Joined: 06 Dec 2005 Posts: 21 Location: Asheville
|
Posted: Wed Jan 11, 2006 10:37 am Post subject: |
|
|
Pretty positive, if I printf buffer above
| Code: |
// adjust buffer, if needed
len += count;
if (len + 256 > capacity) {
capacity *= 2;
buffer = realloc(buffer, capacity);
if (!buffer) break;
}
|
All I get is the http header information, and then the the characters up to the first nul.
Michael |
|
| Back to top |
|
 |
ufoz
Joined: 10 Nov 2005 Posts: 86 Location: Tokyo
|
Posted: Wed Jan 11, 2006 10:46 am Post subject: |
|
|
| planetusa wrote: | Pretty positive, if I printf buffer above
| Code: |
// adjust buffer, if needed
len += count;
if (len + 256 > capacity) {
capacity *= 2;
buffer = realloc(buffer, capacity);
if (!buffer) break;
}
|
All I get is the http header information, and then the the characters up to the first nul.
Michael |
Keep in mind that printf considers \0 as end-of-string. |
|
| Back to top |
|
 |
planetusa
Joined: 06 Dec 2005 Posts: 21 Location: Asheville
|
Posted: Wed Jan 11, 2006 2:19 pm Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
dbeyer3069
Joined: 19 Dec 2005 Posts: 81
|
Posted: Thu Jan 12, 2006 6:47 am Post subject: |
|
|
| planetusa wrote: | | 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.
David Beyer |
|
| Back to top |
|
 |
Dr. Vegetable
Joined: 14 Nov 2005 Posts: 171 Location: Boston, Massachusetts
|
Posted: Thu Jan 12, 2006 11:33 pm Post subject: |
|
|
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.
2. Use printf() to display binary data:
| Code: |
// 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]);
|
Hope this helps! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|