🎓 BookMCQ
← Back to 25. Introduction to Application Layer

📝 Iterative Programming in C General Issues (12 MCQs)

Reliable Control of Repeated Socket Operations

An iterative C network program repeatedly performs communication while preserving correct state between operations. Its control loop must distinguish ordinary input, peer shutdown, timeout, and errors, then decide whether to retry, continue, or release resources. The main difficulty is not the loop syntax but maintaining valid descriptors, buffer lengths, and protocol state. The same discipline applies to both stream-oriented and datagram-oriented programs.

Input Validation, Buffer Limits and Address Conversion

Network input is untrusted byte data, so programs must validate lengths before accessing fields or copying into fixed-size buffers. Use explicit buffer capacities and avoid assuming that received data ends with a null character. Address and port values also require correct conversion between host byte order and network byte order.

Handling Partial Transfers and Interrupted Calls

TCP send and receive operations may transfer fewer bytes than requested, so a program that needs a complete message must loop until the required length is transferred or an error occurs. System calls can also be interrupted, and applications should handle retryable conditions carefully without repeating non-idempotent work blindly. A zero return on stream receive indicates an orderly end of input.

A Fixed-Length TCP Message Read

If a protocol specifies a 12-byte header, a C client should keep reading until all 12 bytes arrive or the connection closes. One recv call may return only five bytes, followed by seven in a later call. Treating the first result as a complete header would misalign subsequent parsing. A correct loop advances its buffer pointer after every successful read.

Resource Management and Protocol Design

Every accepted socket and allocated buffer needs a clear owner and cleanup path. Programs should close descriptors on success and error, limit blocking waits with suitable timeouts, and define how messages are framed over TCP. UDP preserves datagram boundaries, but large or lost datagrams still require careful size and recovery policies.

Misconceptions in C Network Programming

'TCP receive returns one application message' is false because TCP exposes a byte stream. 'A zero byte count always means an error' is incorrect for stream receive because it indicates orderly peer shutdown. 'Network input is already safe to parse' ignores malformed lengths and untrusted content. Applications must use the returned byte count before inspecting buffer contents.

Debugging Loops, Buffers and Socket Lifetimes

Examination problems often ask what happens after partial reads, zero-length returns, negative results, or buffer overflow. Trace descriptor ownership across accept and close, verify network-byte-order conversions, and identify the difference between datagram and stream behavior. Explain how explicit framing and complete-transfer loops prevent incorrect message parsing. Pay particular attention to cleanup after failures occurring midway through an exchange.

C Network Programming Reliability Terms

  • Partial transfer - Operation moves fewer bytes than requested.
  • Framing - Rules that identify application message boundaries.
  • Host byte order - Native integer representation used by a machine.
  • Network byte order - Big-endian format used in network protocols.
  • EOF - End-of-stream indication on a closed TCP peer.
  • Resource leak - Allocated resource not released after use.
4
Easy
5
Medium
3
Hard

📝 All Iterative Programming in C General Issues MCQs

Q1. In an iterative TCP server, what does the accept procedure do when the waiting list of connected clients is empty?

A.It returns an error immediately.
B.It blocks until a client appears in the list. ✅
C.It creates a new listening socket.
D.It terminates the server loop.
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The accept call is designed to block when there are no pending connections; it waits for the operating system to place a newly established client on the waiting list, then returns a connected socket, allowing the server to proceed only after a client arrives.

Q2. If the server calls listen before bind, which of the following outcomes is most likely?

A.The server will reject all incoming connections.
B.The operating system will raise a runtime exception.
C.The server will listen on an arbitrary port assigned automatically. ✅
D.The server will ignore the listen request entirely.
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: When listen is invoked without a prior bind, the OS typically assigns an ephemeral port automatically. This allows the socket to become passive, but the chosen port is not under the programmer's control, which may make it difficult for clients to locate the service.

Q3. Suppose an iterative TCP server is modified to fork a child process after each successful accept. Which logical consequence best describes the server’s ability to handle new clients?

A.The parent process will still serve clients one at a time, but children can serve concurrently. ✅
B.The server will become non‑iterative and handle all clients in the parent process.
C.The accept call will no longer block because children pre‑allocate sockets.
D.The server will lose the ability to close client sockets properly.
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Forking a child after accept lets the child handle the connected client while the parent returns to the accept loop. The parent therefore continues to accept one client at a time, but the children can operate in parallel, giving the system concurrent handling capability.

Q4. How does the flow diagram for iterative TCP communication differ from the UDP diagram regarding socket usage?

A.TCP uses a separate listening socket and a data‑transfer socket; UDP uses only one socket. ✅
B.UDP requires a listen call while TCP does not.
C.TCP creates a socket after each data packet; UDP creates sockets only at connection start.
D.Both protocols use identical socket sequences.
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: TCP establishes a passive listening socket for connection setup, then creates a distinct socket for each accepted client to transfer data. UDP, being connectionless, typically uses a single socket for both sending and receiving, so the diagram shows fewer distinct socket states.

Q5. What would be the impact of omitting the bind step on the server’s listening socket in the iterative TCP model?

A.The server would automatically bind to port 0, making it inaccessible to clients.
B.The server would fail to receive any connection requests because the socket has no local address. ✅
C.The server would still accept connections, but only from the localhost.
D.The server would reuse the client’s address for listening.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Without a bind, the socket lacks a defined local IP address and port, so the operating system cannot associate incoming SYN packets with it. Consequently, connection attempts from clients cannot be matched to the server, and the listen socket remains ineffective.

Q6. When multiple clients attempt to connect simultaneously to an iterative TCP server, which statement correctly characterizes the server’s behavior?

A.All clients are accepted at once and served in parallel.
B.The first client is accepted; subsequent clients remain in the pending queue until the server finishes the current iteration. ✅
C.The server randomly selects one client from the pending queue for each iteration.
D.The server discards excess connection attempts after the first one.
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The iterative server accepts a single pending connection per loop iteration. Additional connection requests are placed on the OS‑maintained pending queue and will be served only after the current client’s data transfer completes and the server returns to the accept call.

Q7. Which step in the iterative TCP server ensures that the server can accept a new client after finishing the current client’s data transfer?

A.Calling destroy_socket on the client socket.
B.Invoking listen again inside the loop.
C.Executing accept at the start of each loop iteration. ✅
D.Re‑binding the listening socket to a new port.
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Placing accept at the beginning of each iteration makes the server wait for the next pending client. After the previous client’s socket is closed, the loop returns to accept, which either immediately returns a queued client or blocks until a new one arrives.

Q8. Arrange the following socket states in the order they occur for a single client in an iterative TCP server: (1) Listening socket created, (2) Data‑transfer socket returned by accept, (3) Socket bound to a local address, (4) Socket closed after termination.

A.1 → 3 → 2 → 4 ✅
B.3 → 1 → 2 → 4
C.1 → 2 → 3 → 4
D.1 → 3 → 4 → 2
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: First the server creates and binds a listening socket (state 1 and 3). When a client connects, accept returns a new data‑transfer socket (state 2). After the communication ends, the server closes that socket (state 4), completing the sequence.

Q9. After accept returns a new socket for a client, which fields must be correctly populated to enable reliable data exchange?

A.Only the remote socket address field.
B.Both the remote socket address and the local port number. ✅
C.The remote socket address, the local address, and the protocol identifier.
D.No fields need to be set; the OS fills them automatically.
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The newly created socket inherits the local port used by the listening socket, but the remote address (client’s IP and port) must be set so the server knows where to send data. Both pieces are essential for a bidirectional TCP stream.

Q10. What is the primary purpose of the listen procedure in a TCP server?

A.To create a socket for data transmission.
B.To bind a socket to a specific IP address.
C.To mark the socket as passive and allow the OS to queue incoming connection requests. ✅
D.To terminate an existing connection.
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Listen converts a bound socket into a passive listening socket, signaling the operating system to accept incoming SYN packets and place completed connection requests on a pending queue for later accept calls.

Q11. If the server’s accept call is placed outside the infinite loop, what logical effect does this have on handling multiple clients?

A.The server will accept exactly one client and then terminate.
B.The server will accept one client and then repeatedly serve the same client forever. ✅
C.The server will accept a client, close the socket, and then accept another client automatically.
D.The server will block forever after the first client disconnects.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: When accept is executed only once before the loop, the returned client socket is reused for every iteration. Consequently, the server continues to interact with that single client, never invoking accept again to obtain new connections.

Q12. Comparing iterative and concurrent TCP server designs, which advantage is unique to the iterative approach?

A.Ability to handle many clients simultaneously without additional threads.
B.Simpler resource management because only one client socket is active at a time. ✅
C.Reduced latency for each client due to parallel processing.
D.Automatic load balancing across multiple CPU cores.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Iterative servers handle one client at a time, so they avoid the overhead of thread or process creation and synchronization. This simplicity reduces resource consumption and makes debugging easier, albeit at the cost of concurrency.

🔗 Related Topics (MCQs)