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:

  1. encodes text as UTF-8 bytes;
  2. decodes the bytes back into a string;
  3. 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:

  1. opens a TCP socket;
  2. connects to (host, port);
  3. sends message as one UTF-8 line ending with \n;
  4. receives bytes until a reply line ending with \n has arrived;
  5. closes the socket;
  6. 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:

  1. binds to (host, port);
  2. listens for one client;
  3. accepts one connection;
  4. receives one UTF-8 line ending with \n;
  5. sends back "RECEIVED:" + message + "\n";
  6. 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:" + message to 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 = 5000

Write 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:ID

The function should:

  • return (command, id_number) if the message has exactly one colon and the ID is an integer;
  • return None for 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:

RequestResponse
PINGPONG
GET:<id>ITEM <id>
any other requestERROR

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:

  1. attempts to open a TCP connection with a short timeout;
  2. returns "CONNECTED" if the connection succeeds;
  3. returns "CONNECTION FAILED" if the connection raises an OSError.

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.