Socket Server Lifecycle
Run the Socket Server Lifecycle MicroSim Fullscreen
About This MicroSim
A socket is a software endpoint for sending and receiving data over a network. Your robot's web server uses one socket to wait for browsers, and a new socket for each browser that connects.
The right column shows the server calls from the complete web server in Chapter 11, top to bottom:
s = socket.socket()creates the server socket.s.bind(addr)claims port 80, the standard web port.s.listen(1)starts listening. One browser may wait in line.- Inside the
while Trueloop,s.accept()blocks. That means the program stops and waits right there until a browser connects. accept()returns a new socket,conn, for just that browser.conn.recv(1024)reads the browser's request.handle_cmd(cmd)runs a motor command, but only for a POST withcmd=.conn.send(html_page())sends the page back.conn.close()hangs up, and the loop goes back toaccept().
The left column shows the same conversation from the browser's side, with arrows for each network message. The listen(1) queue at the bottom holds one waiting browser. Any more are refused.
How to Use
- Press Next Step three times to run
socket(),bind(), andlisten(). - Press Next Step again. The server is now stuck in
accept(): watch the red WAITING (blocked) tag and the stopwatch. - Press Browser A connects.
accept()returns right away and the server moves on. - Pick POST cmd=forward in Request type before the
recv()step, then keep stepping. The robot's wheels start to turn. - While the server is busy with Browser A, press Browser B connects twice. Where does each one go?
- Uncheck Add s.close() at the end, step past
bind(), press Reset, and run the server again. What happens atbind()?
Try this challenge: Press "Browser A connects", "Browser B connects", and "Browser B connects" again before the server finishes with A. What happens to the second B, and which number in the code controls this?
Iframe Embed Code
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan
Learning Objective
Students will sequence the socket calls a web server makes (socket, bind, listen, accept, recv, send, close), and will explain which call blocks and why a single-threaded robot server handles one browser at a time (Bloom's Taxonomy: Apply).
Grade Level
Grades 8–12
Duration
15–20 minutes
Prerequisites
- HTTP GET and POST requests and socket programming from Chapter 11: Wireless Networking and Web Servers.
while Trueloops andtry/finallyfrom Chapter 4: Control Flow, Functions, and Exception Handling.- Motor functions such as
go_forward()from Chapter 7: PWM, Motor Speed Control, and Actuators.
Activities
- Predict the order (3 min). Students list the server calls in the order they expect them to run, then step through the sim to check. Ask which calls run once and which run on every loop.
- Blocking (4 min). Students stop at
accept()and watch the stopwatch. Discuss: "While the robot waits here, can it read its distance sensor?" This motivates why the controller page must be small and fast, and previews non-blocking designs. - One browser at a time (5 min). Students connect Browser A, then press Browser B twice while A is being served. They record where each B goes and connect the result to
listen(1). - Request types (3 min). Students serve one GET and two POST requests and note that only a POST with
cmd=calls a motor function, and that the motor keeps running after the connection closes. - Cleanup (3 min). Students run the
s.close()experiment and explain why thefinallyblock matters when you restart a program during development.
Assessment
- Formative: Point at the stopwatch during Activity 2 and ask, "Which line of code is running right now, and what is it waiting for?"
- Exit ticket: "Two classmates open the robot's page at the same moment, and a third opens it a second later. Describe what happens to each browser, and name the line of code that decides it."
- Rubric (4-point): Exemplary — correct order for all nine steps, accurate explanation of blocking in
accept(), the role oflisten(1), and whys.close()belongs infinally. Proficient — correct order and a correct explanation of blocking and the queue. Developing — correct setup order but confusion between the server socketsand the connection socketconn. Beginning — cannot order the calls or identify the blocking call.
References
- Chapter 11: Wireless Networking and Web Servers — the complete web server program this sim follows.
- Socket Lifecycle Diagram (Networking course) — the two-column client/server lifecycle layout this sim is based on.
- MicroPython
socketmodule — documentation forbind(),listen(),accept(),recv(),send(), andclose(). - Socket Programming HOWTO — Python's guide to server sockets and blocking calls.
- Berkeley sockets — Wikipedia article on the socket interface used by almost every network program.
- Port (computer networking) — Wikipedia article on ports, including port 80 for HTTP.