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?
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 |
Why do I keep getting EINTR from the socket calls?
How should I choose a port number for my server?
What are the pros/cons of select(), non-blocking I/O and SIGIO?
What pieces of information make up a socket?
system choose one for me on the connect() call? Should I bind() a port number in my client program, or let the?
How do I get the port number for a given service?
How do Sockets Work?
Is tcp or unix socket faster?
Why does it take so long to detect that the peer died?
How can I read ICMP errors from connected UDP sockets?
What exactly does SO_KEEPALIVE do?
What is socket address with example?