Chapter 35 Key Takeaways
TCP/IP Fundamentals
- TCP provides reliable, ordered, connection-oriented communication
- UDP provides fast, unreliable, connectionless communication
- Sockets are the programming interface for network I/O — like file handles for the network
- A server listens on a port; a client connects to an IP + port
Free Pascal Socket Programming
| Unit | Classes | Purpose |
|---|---|---|
ssockets |
TInetSocket, TInetServer |
TCP client and server |
fphttpclient |
TFPHTTPClient |
HTTP GET/POST requests |
fpjson |
TJSONObject, TJSONArray |
JSON for API data |
HTTP Essentials
- Request: Method (GET/POST) + Path + Headers + Body
- Response: Status Code + Headers + Body
- Headers and body separated by a blank line (
\r\n\r\n) - Common status codes: 200 OK, 201 Created, 400 Bad Request, 404 Not Found, 500 Server Error
REST API Pattern
| Operation | HTTP Method | URL Pattern | Returns |
|---|---|---|---|
| List | GET | /api/resources |
JSON array |
| Get one | GET | /api/resources/42 |
JSON object |
| Create | POST | /api/resources |
201 + created object |
| Update | PUT | /api/resources/42 |
Updated object |
| Delete | DELETE | /api/resources/42 |
204 No Content |
MicroServe Architecture
- Listen on a port for TCP connections
- Read raw bytes from the socket
- Parse the HTTP request (method, path, headers, body)
- Route to a handler function based on method + path
- Handler generates a response (HTML, JSON, error)
- Format the HTTP response (status line + headers + body)
- Write the response to the socket and close
Network Error Handling
- Always wrap network calls in
try..except - Set timeouts (
IOTimeout,ConnectTimeout) — never wait forever - Handle specific exceptions:
EHTTPClient,ESocketError - Provide meaningful error messages for the user
- Retry transient failures; fail gracefully for permanent ones
PennyWise Sync
TSyncClientpushes expenses via POST and pulls via GETTestConnectionverifies server availability before syncing- All operations handle timeouts and network failures gracefully