Paper 2 Computer Networks Drills
These are original Paper 2-style practice questions. They use exact host/port assumptions, protocol messages, packet fragments, and expected output evidence.
Detailed answers are in Paper 2 Computer Networks Answers.
Revise the topic hub first:
Questions
Question 1: Encode Decode
Write a function round_trip(text) that:
- encodes
textas UTF-8 bytes; - decodes the bytes back into a string;
- returns the decoded string.
Test your function with:
print(round_trip("hello"))
print(round_trip("café"))Expected output:
hello
café[4]
Question 2: Simple Client
Write a function send_message(host, port, message) that:
- opens a TCP socket;
- connects to
(host, port); - sends
messageas one UTF-8 line ending with\n; - receives bytes until a reply line ending with
\nhas arrived; - closes the socket;
- returns the decoded reply without the final newline.
Assume a local one-shot test server is already running and replies with "ACK:" + message + "\n".
Test call:
print(send_message("127.0.0.1", port, "HELLO"))Expected output:
ACK:HELLO[8]
Question 3: Simple Server
Write a one-shot TCP server function run_once_server(host, port) that:
- binds to
(host, port); - listens for one client;
- accepts one connection;
- receives one UTF-8 line ending with
\n; - sends back
"RECEIVED:" + message + "\n"; - closes the connection and server socket.
Test it with a local client sending "PING\n".
Expected client-side output:
RECEIVED:PING[10]
Question 4: Echo Server Logic
Write a function echo_messages(messages) that simulates the repeated-message behavior of an echo server.
Rules:
- for each message before
"QUIT", append"ECHO:" + messageto the reply list; - when
"QUIT"is reached, stop immediately; - do not echo
"QUIT"or any later messages.
Test:
print(echo_messages(["red", "blue", "QUIT", "green"]))Expected output:
['ECHO:red', 'ECHO:blue'][12]
Question 5: Port Config
Define constants:
HOST = "127.0.0.1"
PORT = 5000Write a function endpoint() that returns (HOST, PORT).
Test:
print(endpoint())Expected output:
('127.0.0.1', 5000)[4]
Question 6: Protocol Message
Write a function parse_message(message) for messages in this format:
COMMAND:IDThe function should:
- return
(command, id_number)if the message has exactly one colon and the ID is an integer; - return
Nonefor malformed messages.
Test:
print(parse_message("GET:42"))
print(parse_message("BAD"))Expected output:
('GET', 42)
None[6]
Question 7: DNS Lookup
Use this dictionary to simulate DNS lookup:
dns = {
"school.test": "192.0.2.10",
"library.test": "192.0.2.20"
}Write a function lookup(domain) that returns the matching IP address, or "NOT FOUND" if the domain is missing.
Test:
print(lookup("school.test"))
print(lookup("missing.test"))Expected output:
192.0.2.10
NOT FOUND[5]
Question 8: Packet Reassembly
The following packet fragments arrived out of order:
fragments = [(3, "ORL"), (1, "HEL"), (4, "D"), (2, "LOW")]Write a function reassemble(fragments) that sorts by sequence number and joins the payload strings.
Test:
print(reassemble(fragments))Expected output:
HELLOWORLD[8]
Question 9: Client Test
A request-response server should behave as follows:
| Request | Response |
|---|---|
PING | PONG |
GET:<id> | ITEM <id> |
| any other request | ERROR |
Write a function expected_response(message) that returns the expected response for one test message.
Test:
print(expected_response("PING"))
print(expected_response("GET:42"))
print(expected_response("HELLO"))Expected output:
PONG
ITEM 42
ERROR[4]
Question 10: Connection Error
Write a function safe_connect(host, port) that:
- attempts to open a TCP connection with a short timeout;
- returns
"CONNECTED"if the connection succeeds; - returns
"CONNECTION FAILED"if the connection raises anOSError.
Test with a local port that is expected to be closed:
print(safe_connect("127.0.0.1", 1))Expected output:
CONNECTION FAILED[6]
Review Checklist
After attempting these questions, check whether you can:
- encode and decode socket messages as bytes;
- open, use, and close client/server sockets;
- apply a simple message protocol exactly;
- simulate DNS lookup and packet reassembly using suitable data structures;
- show exact expected output for each test case;
- handle connection failure without crashing.