TCP Modes Flashcards

1
Q

What are the three socket modes?

A

Blocking, non-blocking, and timeout. Blocking waits indefinitely, non-blocking returns errors if operations can’t complete immediately, and timeout raises exceptions after a specified duration.

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

What is the default socket mode and timeout?

A

Sockets are created in blocking mode by default with a timeout value of None (no timeout). Use getdefaulttimeout() to check.

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

How does setdefaulttimeout() differ from socket.settimeout()?

A

setdefaulttimeout() sets the global timeout for new sockets. socket.settimeout() sets a timeout for a specific socket without affecting the global default.

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

What does gettimeout() return for each socket mode?

A
  • Blocking: None<br></br>- Non-blocking: 0<br></br>- Timeout: A positive float (timeout duration in seconds).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does the connect() method behave in terms of timeout?

A

connect() always operates in timeout mode. The timeout can be set using socket.settimeout() before calling connect().

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

What determines the mode of the socket returned by accept()?

A

The defaulttimeout value:<br></br>- If defaulttimeout is positive: Returned socket uses timeout mode.<br></br>- If defaulttimeout is 0: Non-blocking.<br></br>- If defaulttimeout is None: Mode matches the listening socket (blocking/timeout) or is OS-dependent (non-blocking).

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

Which socket methods are always blocking regardless of mode?

A

socket(), bind(), and connect() (though connect() behaves as timeout).

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

What happens in non-blocking mode if an operation can’t complete immediately?

A

The method returns an error (e.g., BlockingIOError) instead of waiting.

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

How to configure a socket to raise TimeoutError after 5 seconds?

A

Use socket.settimeout(5.0) to set a per-socket timeout or setdefaulttimeout(5.0) for global configuration.

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

What is the purpose of SO_LINGER in the close() method?

A

It configures whether close() blocks to ensure data is sent. By default, close() is non-blocking unless SO_LINGER is set.

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

How does the create_connection() method handle timeouts?

A

It uses the timeout parameter (defaults to getdefaulttimeout()). If the default is non-positive, the OS assigns a timeout.

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

What error message does the time_client example print for a connect timeout?

A

time(client): connect timeout if the connection exceeds the 3-second timeout.

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

How to differentiate blocking and timeout modes using getblocking()?

A

getblocking() returns True for blocking and timeout modes (since timeout is a subtype of blocking), and False for non-blocking.

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

What exception is raised when a timeout expires during recv()?

A

TimeoutError (e.g., time(client): no response from server in the example).

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

Why might fixed-size messaging be inefficient for TCP?

A

TCP already handles fragmentation; adding fixed-size messages duplicates this effort (“TCP over TCP”), increasing overhead.

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