Sockets (TCP Client and Server)
sequenceDiagram participant S as Server participant C as Client S->>S: socket() S->>S: bind((host, port)) S->>S: listen(backlog) S->>S: accept() -- blocks here C->>C: socket() C->>S: connect((host, port)) S-->>S: accept() returns a NEW socket for this client C->>S: sendall(b"ping") S->>S: recv(1024) S->>C: sendall(b"pong") C->>C: recv(1024) C->>S: close() S->>S: close() the per-client socket, keep listening
What is a socket?
Section titled “What is a socket?”A socket is an endpoint for network communication.
For TCP:
- server listens on (host, port)
- client connects to (host, port)
Simple TCP server
Section titled “Simple TCP server”import socket
HOST = "127.0.0.1"
PORT = 5050
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f"Listening on {HOST}:{PORT}")
conn, addr = s.accept()
with conn:
print("Connected by", addr)
data = conn.recv(1024)
print("received:", data.decode())
conn.sendall(b"Hello client")Simple TCP client
Section titled “Simple TCP client”import socket
HOST = "127.0.0.1"
PORT = 5050
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello server")
data = s.recv(1024)
print("received:", data.decode())- Use
127.0.0.1for local experiments. - Choose ports above 1024.
- Add timeouts (
s.settimeout(5)) to avoid hanging.
Check yourself
Section titled “Check yourself”-
A client calls `sendall(b'HELLO')` then `sendall(b'WORLD')`. A receiver loops on `recv(4)`. What does it get?
Measured. TCP delivers a byte stream — the send boundaries are not preserved, so one send can be split across reads and two sends can merge into one.
pch.quizShowAnswer
B — `[b'HELL', b'O', b'WORL']` — Measured. TCP delivers a byte stream — the send boundaries are not preserved, so one send can be split across reads and two sends can merge into one.
-
`recv(1024)` returns 12 bytes. What does that mean?
A short read is ordinary, not exceptional. Only a return of zero bytes means the peer closed the connection.
pch.quizShowAnswer
C — That is normal — it returns what is available — A short read is ordinary, not exceptional. Only a return of zero bytes means the peer closed the connection.
-
Which of these is NOT a valid way to frame messages over TCP?
That assumption is precisely what the measurement disproves. It appears to work on localhost with small payloads, which is why it survives testing.
pch.quizShowAnswer
D — Assuming each `recv` returns exactly one `sendall` — That assumption is precisely what the measurement disproves. It appears to work on localhost with small payloads, which is why it survives testing.
-
What does `accept()` return on a listening server socket?
The listening socket keeps listening; the new socket carries the conversation. Serving only one client is the usual symptom of reusing the listening socket by mistake.
pch.quizShowAnswer
B — A NEW socket for that one client, plus its address — The listening socket keeps listening; the new socket carries the conversation. Serving only one client is the usual symptom of reusing the listening socket by mistake.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – HTTP GET with urllib
Section titled “Exercise 1 – HTTP GET with urllib”Exercise 2 – Socket Connection
Section titled “Exercise 2 – Socket Connection”Exercise 3 – Resolve a Hostname
Section titled “Exercise 3 – Resolve a Hostname”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading