📝 Iterative Programming in C (8 MCQs)
Repeating Client and Server Operations in C
Iterative network programs in C use loops to repeat communication tasks, such as accepting successive clients or receiving successive datagrams. The loop controls application behavior, while socket functions provide access to transport services. Clear separation between setup, request processing, response generation, and cleanup makes the program easier to trace and maintain. This structure is useful for small services with predictable request-processing time.
Socket Descriptors, Addresses and Buffers
On POSIX systems, socket returns a file descriptor used by later calls. IPv4 endpoints are commonly represented by sockaddr_in, with address and port values stored in network byte order. Character buffers hold application data, and their lengths must be tracked explicitly because received data is not automatically a null-terminated C string.
Looping Through Receive and Send Operations
A server creates and configures its socket before entering a loop. Each iteration receives input, checks the returned byte count, processes only the valid bytes, and sends the corresponding response. For a TCP server, the loop may surround accept so that a new client can be handled after the previous connected socket is closed.
A Repeated UDP Request Loop
A C server can use recvfrom inside a while loop to receive a datagram and capture the client's sockaddr_in address. After validating the received length, it calls sendto with that address to return a reply. The loop then waits for another datagram, allowing the same bound socket to serve successive clients.
C Runtime and Operating-System Responsibilities
C code manages descriptors, memory buffers, address structures, and function return values, while the operating system maintains socket state and protocol processing. Calls may be interrupted or fail because of invalid arguments, closed peers, resource limits, or network conditions. Correct programs inspect results and release descriptors when their role ends.
Common C Networking Loop Errors
'A recv buffer is always a string' is incorrect because network data is a byte sequence and may contain zero bytes. 'A successful send transmits the whole buffer' is unsafe for TCP because sends can be partial. 'A closed client descriptor can be reused freely' ignores descriptor lifetime and the need to check close and later creation results.
C Socket Flow and Return-Value Questions
Be ready to identify socket types, address structures, descriptor use, and the sequence of calls inside an iterative loop. Interpret positive, zero, and negative results for receive operations according to the function and transport. Check lengths, byte-order conversions, cleanup paths, and whether the loop handles a client, a datagram, or a connection request.
C Socket Programming Terms
- File descriptor - Integer handle returned for an open socket.
- sockaddr_in - Structure describing an IPv4 socket address.
- recvfrom - Reads a datagram and records its sender.
- sendto - Sends a datagram to a chosen endpoint.
- Network byte order - Standard big-endian representation for protocol values.
- Buffer - Memory area holding received or outgoing bytes.
📝 All Iterative Programming in C MCQs
Q1. How many fields are defined in the C socket structure as described in the passage?
📖 Explanation: The passage explicitly states that the C socket is defined as a structure consisting of five fields. Therefore, the correct count is five, making option B the accurate answer. This count includes the address family, port number, IP address, protocol, and a reserved field, which together form the complete socket descriptor in C.
Q2. If a socket has no internal buffer, which component must the operating system provide to enable successful data transmission?
📖 Explanation: Because the socket itself holds no storage, the kernel must allocate buffers on its behalf. These kernel‑level buffers temporarily hold outgoing and incoming bytes, allowing the socket to interface with the network hardware. Without such buffers, the application would have nowhere to place data for transmission, so option A is correct.
Q3. Which statement best compares a C socket to a Unix file descriptor in terms of their abstraction role?
📖 Explanation: Both sockets and file descriptors are opaque identifiers that the kernel uses to reference underlying resources. They hide implementation details from the programmer, allowing uniform system‑call interfaces for I/O. This conceptual similarity makes option D the most accurate description.
Q4. In an iterative server written in C, which pattern correctly implements handling of successive client connections without spawning new threads?
📖 Explanation: An iterative server typically runs a single loop: it calls accept() to obtain a client socket, processes the request sequentially, closes the connection, and then returns to accept() for the next client. This pattern avoids thread creation and keeps the control flow simple, matching option B.
Q5. When the recv() function returns 0 for a connected socket, what can be logically inferred about the peer?
📖 Explanation: A return value of zero from recv() indicates that the remote endpoint has closed its sending side gracefully. This is the standard signal of an orderly shutdown, meaning no more data will arrive. Therefore, the correct inference is option B.
Q6. If a server switches from a multithreaded design to a single‑threaded design using select(), what is the most direct effect on CPU utilization under high connection load?
📖 Explanation: Using select() removes the overhead of creating and scheduling multiple threads. The kernel monitors multiple descriptors within a single thread, so the costly context‑switch operations disappear. Consequently, overall CPU consumption tends to drop, especially when many connections are active, making option A the correct outcome.
Q7. How can an iterative C program be adapted to work with non‑blocking sockets while still preserving the sequential processing order of client requests?
📖 Explanation: A non‑blocking socket returns EAGAIN when no data is available. By implementing a state machine that repeatedly attempts recv() and only proceeds when data is received, the program can keep the logical order of processing while avoiding blocking. This approach matches option B.
Q8. Considering the five‑field socket structure, which field is most critical for ensuring that two processes on different hosts can locate each other?
📖 Explanation: The IP address field identifies the network host on which a process resides, enabling remote machines to route packets correctly. While the port number distinguishes services on that host, the fundamental ability to locate the host itself depends on the IP address, making option D the most essential field for cross‑host discovery.