Network Connection drops upon receipt of NUL character

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
planetusa
Posts: 21
Joined: Tue Dec 06, 2005 3:03 pm
Location: Asheville
Contact:

Network Connection drops upon receipt of NUL character

Post by planetusa »

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: Select all

   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 &#40;count < 0&#41; &#123; 
         pspDebugScreenPrintf&#40;"read error&#58; %i\n", errno&#41;; 
         break; 
      &#125; 

      // adjust buffer, if needed 
      len += count; 
      if &#40;len + 256 > capacity&#41; &#123; 
         capacity *= 2;
         buffer = realloc&#40;buffer, capacity&#41;; 
         if &#40;!buffer&#41; break; 
      &#125; 
   &#125; 
Thanks,
Michael
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

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
planetusa
Posts: 21
Joined: Tue Dec 06, 2005 3:03 pm
Location: Asheville
Contact:

Post by planetusa »

Pretty positive, if I printf buffer above

Code: Select all

      // adjust buffer, if needed 
      len += count; 
      if &#40;len + 256 > capacity&#41; &#123; 
         capacity *= 2; 
         buffer = realloc&#40;buffer, capacity&#41;; 
         if &#40;!buffer&#41; break; 
      &#125; 
All I get is the http header information, and then the the characters up to the first nul.

Michael
ufoz
Posts: 86
Joined: Thu Nov 10, 2005 2:36 am
Location: Tokyo
Contact:

Post by ufoz »

planetusa wrote:Pretty positive, if I printf buffer above

Code: Select all

      // adjust buffer, if needed 
      len += count; 
      if &#40;len + 256 > capacity&#41; &#123; 
         capacity *= 2; 
         buffer = realloc&#40;buffer, capacity&#41;; 
         if &#40;!buffer&#41; break; 
      &#125; 
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.
planetusa
Posts: 21
Joined: Tue Dec 06, 2005 3:03 pm
Location: Asheville
Contact:

Post by planetusa »

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
dbeyer3069
Posts: 81
Joined: Mon Dec 19, 2005 4:09 pm

Post by dbeyer3069 »

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
Dr. Vegetable
Posts: 171
Joined: Mon Nov 14, 2005 1:32 am
Location: Boston, Massachusetts
Contact:

Post by Dr. Vegetable »

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: Select all

// Assumes 'buffer' is the array of received bytes
// and 'len' is the total number of bytes received
for &#40;int n=0; n<len; n++&#41;
   printf&#40;"%02X ", buffer&#91;n&#93;&#41;;
Hope this helps!
Post Reply