Reputation: 1413
We know we should use socket.read(buffer, offset, count) to read network data, but I have a question, where the data exactly stored before we read them? is there are stores in memory card or network adapter card or someplace else?, assume the remote device sent us 100KB each seconds, and our device received them, but we DID NOT invoke the method to read it, then, may something happen? or just the data disappear?
Upvotes: 2
Views: 124
Reputation: 5828
Typically the data that has arrived on the connection but has not yet been read by the application is stored in a buffer in kernel memory. When the application calls socket.read
the requested amount of data is copied out of the kernel buffer into the application's buffer.
The size of the kernel buffer is limited, so it can not consume unbounded amounts of kernel memory if the application stops making read requests. Depending on the type of socket you have, buffer overflow is prevented by telling the sender that no more data can be accepted (for a TCP socket) or by discarding data (for a UDP socket or local datagram socket) or by blocking the sending process (for a local stream socket).
Data that has been stored in the kernel buffer but not read by the application is kept in the buffer until the socket is closed by the app or until the app exits. At that point any unread data is discarded and the kernel buffer is released.
Upvotes: 3