TCP Data Exchange Flashcards
What is the main method for receiving data in TCP connections?
socket.recv(bufsize), which returns a bytes object. The bufsize parameter (preferably a power of 2) determines the max bytes to receive. It blocks if the buffer is empty and reads up to bufsize bytes if data is available.
How does send(bytes) differ from sendall(bytes)?
send() returns the number of bytes actually sent (may not send all), while sendall() ensures all bytes are sent or raises an exception. Beginners are advised to use sendall() for simplicity.
Why is assuming a 1:1 ratio between send() and recv() operations a misconception?
TCP aggregates or splits data based on network conditions. A single send() may require multiple recv() calls, and multiple send() operations might be read in one recv().
What are the four solutions to determine the “end of a message” in TCP?
- Use a large buffer size (risky due to resource waste); 2. Prefix the message with its length (e.g., “30-…”); 3. Add an end delimiter (e.g., “xxx”); 4. Use fixed-size messages (with padding if needed).
What problem arises when using a delimiter like “xxx” for message termination?
The delimiter might appear in the actual data, causing ambiguity. Careful delimiter selection and error handling (e.g., timeouts) are required.
How does the recv_into(buffer) method work?
It reads data into a pre-allocated buffer instead of returning a bytes object. Returns the number of bytes received, up to the buffer’s size.
What are the three methods to signal the “end of a session”?
- Disconnect using shutdown(); 2. Use an end-of-session delimiter; 3. Implement an application-layer protocol (e.g., “end” command, timeout, or message count).
Why is prefixing messages with their length a robust solution?
It informs the receiver exactly how many bytes to expect, simplifying data assembly. Example: “30-Please Transfer…” where “30” is the byte count.
What is the risk of relying on shutdown() to end a session?
Buffered data might not be flushed properly, especially in non-blocking sockets. TCP’s FIN signal may not resolve all edge cases.
Which socket methods are UDP-specific?
recvfrom(), sendto(), and recvfrom_into(). These handle connectionless communication with address tracking.
What is the purpose of sendfile(file)?
To efficiently send an entire file over TCP by reading and transmitting its contents in binary mode until EOF. Uses high-performance OS-level operations.
Why is fixed-size messaging sometimes impractical?
Requires padding for small messages and fragmentation for large ones, adding overhead. Fragmentation duplicates TCP’s role, leading to inefficiency (“TCP over TCP”).
How does TCP ensure data integrity despite network unpredictability?
TCP guarantees in-order delivery and reliability via acknowledgments, retransmissions, and error checking. However, message boundaries are not preserved.
What is the MTU for Ethernet, and why does it matter?
1500 bytes (data limited to ~1448 bytes after headers). Affects TCP segment sizing, as messages exceeding MTU are fragmented at the network layer.
When should UDP be considered instead of TCP?
For single-message sessions, real-time applications (e.g., VoIP), or when overhead must be minimized. Reliability can be added at the application layer if needed.