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

Why does the sockets buffer fill up sooner than expected?

0 Answers  


What is the difference between SO_REUSEADDR and SO_REUSEPORT?

0 Answers  


How come select says there is data, but read returns zero?

1 Answers  


What pieces of information make up a socket?

0 Answers  


What is socket programming in java?

0 Answers  






Why do I get connection refused when the server is not running?

1 Answers  


Is a socket a file?

0 Answers  


Is there any advantage to handling the signal, rather than just ignoring it and checking for the EPIPE error? Are there any useful parameters passed to the signal catching function?

0 Answers  


Is socket a hardware or software?

0 Answers  


How do I convert a string into an internet address?

0 Answers  


What is active unix domain sockets?

0 Answers  


What is a socket address?

0 Answers  


Categories