TCP Data Exchange Flashcards

1
Q

What is the main method for receiving data in TCP connections?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does send(bytes) differ from sendall(bytes)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is assuming a 1:1 ratio between send() and recv() operations a misconception?

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the four solutions to determine the “end of a message” in TCP?

A
  1. 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What problem arises when using a delimiter like “xxx” for message termination?

A

The delimiter might appear in the actual data, causing ambiguity. Careful delimiter selection and error handling (e.g., timeouts) are required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the recv_into(buffer) method work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the three methods to signal the “end of a session”?

A
  1. Disconnect using shutdown(); 2. Use an end-of-session delimiter; 3. Implement an application-layer protocol (e.g., “end” command, timeout, or message count).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why is prefixing messages with their length a robust solution?

A

It informs the receiver exactly how many bytes to expect, simplifying data assembly. Example: “30-Please Transfer…” where “30” is the byte count.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the risk of relying on shutdown() to end a session?

A

Buffered data might not be flushed properly, especially in non-blocking sockets. TCP’s FIN signal may not resolve all edge cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which socket methods are UDP-specific?

A

recvfrom(), sendto(), and recvfrom_into(). These handle connectionless communication with address tracking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of sendfile(file)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is fixed-size messaging sometimes impractical?

A

Requires padding for small messages and fragmentation for large ones, adding overhead. Fragmentation duplicates TCP’s role, leading to inefficiency (“TCP over TCP”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does TCP ensure data integrity despite network unpredictability?

A

TCP guarantees in-order delivery and reliability via acknowledgments, retransmissions, and error checking. However, message boundaries are not preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the MTU for Ethernet, and why does it matter?

A

1500 bytes (data limited to ~1448 bytes after headers). Affects TCP segment sizing, as messages exceeding MTU are fragmented at the network layer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When should UDP be considered instead of TCP?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly