Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


over the socket? Is there a way to have a dynamic buffer? What does one do when one does not know how much information is coming?



over the socket? Is there a way to have a dynamic buffer? What does one do when one does not know ho..

Answer / chaitanya

When the size of the incoming data is unknown, you can either make the size of the buffer as big as the largest possible (or likely) buffer, or you can re-size the buffer on the fly during your read. When you malloc() a large buffer, most (if not all) varients of unix will only allocate address space, but not physical pages of ram. As more and more of the buffer is used, the kernel allocates physical memory. This means that malloc'ing a large buffer will not waste resources unless that memory is used, and so it is perfectly acceptable to ask for a meg of ram when you expect only a few K.

On the other hand, a more elegant solution that does not depend on the inner workings of the kernel is to use realloc() to expand the buffer as required in say 4K chunks (since 4K is the size of a page of ram on most systems). I may add something like this to sockhelp.c in the example code one day.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Unix Socket Programming Interview Questions

How can I read ICMP errors from connected UDP sockets?

1 Answers  


What are socket exceptions? What is out-of-band data?

1 Answers  


What is a deep well socket?

0 Answers  


Can a single socket port be used for multiple applications?

0 Answers  


What are raw sockets?

4 Answers   Axis Technologies,


How can I write a multi-homed server?

0 Answers  


How would I put my socket in non-blocking mode?

1 Answers  


How come I get address already in use from bind()?

1 Answers  


How do I get my server to find out the clients address / host- name?

1 Answers  


How do I send [this] over a socket?

1 Answers  


What exactly does SO_LINGER do?

1 Answers  


What are the types of sockets?

0 Answers  


Categories