core: add all the required files
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
# Typing script — reconstruct `mini_serv` cold, stage by stage
|
||||
|
||||
Your situation: exam in ~3 days, never wrote a socket server, 1–2 h/day.
|
||||
So the strategy is **not** "invent it" and **not** "understand every syscall".
|
||||
It is: **replay this exact typing order** until your hands know it. After every stage
|
||||
there is a checkpoint. If a checkpoint fails, fix that stage before moving on.
|
||||
|
||||
> Time budget per stage is for your *third* rep. First rep: double it. Don't rush the first one.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
A["A: top of file<br/>checkpoint: compiles clean"] --> B["B: guard + socket/bind/listen<br/>checkpoint: args error + LISTEN in ss"]
|
||||
B --> C["C: loop + accept + broadcast<br/>checkpoint: two nc windows see 'just arrived'"]
|
||||
C --> D["D: recv + buffer + relay<br/>checkpoint: hello relays; split line stays silent"]
|
||||
D --> E["E: disconnect<br/>checkpoint: Ctrl-D gives 'just left'; partial dropped"]
|
||||
E --> F["F: bash tester/check.sh<br/>checkpoint: 13/13 + GRADEME PASSED"]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Stage 0 — setup (1 min)
|
||||
|
||||
- In the exam: you write in the `rendu` directory the shell shows you (ask the shell).
|
||||
- Here: create `sandbox/rendu/mini_serv/mini_serv.c`.
|
||||
- Open `subject/subject.en.txt` and `subject/main.c` in another tab — **in the exam you have both**, use them.
|
||||
|
||||
## Stage A — top of file (10 min)
|
||||
|
||||
Type, in this order:
|
||||
|
||||
1. the 7 includes (`stdlib, string, unistd, stdio, sys/socket, netinet/in, sys/select`)
|
||||
2. the struct:
|
||||
```c
|
||||
typedef struct s_client { int fd; int id; char *buf; } t_client;
|
||||
```
|
||||
3. the 4 globals: `g_c[FD_SETSIZE]`, `g_maxfd`, `g_server`, `g_next_id`
|
||||
4. `fatal()`:
|
||||
```c
|
||||
write(2, "Fatal error\n", 12); exit(1);
|
||||
```
|
||||
|
||||
**Checkpoint A:** `cc -Wall -Wextra -Werror mini_serv.c -o mini_serv` → no output, no errors.
|
||||
(If the compiler lists an unused something: that's Stage A material, not exam material — fix it now.)
|
||||
|
||||
## Stage B — main(), part 1: guard + socket (15 min)
|
||||
|
||||
1. `if (argc != 2)` → `write(2, "Wrong number of arguments\n", 26);` → `return (1);`
|
||||
2. `g_server = socket(AF_INET, SOCK_STREAM, 0);` + `if (< 0) fatal();`
|
||||
3. `struct sockaddr_in addr;` → `bzero(&addr, sizeof(addr));`
|
||||
4. `addr.sin_family = AF_INET;`
|
||||
5. `addr.sin_addr.s_addr = htonl(2130706433);` ← **the 127.0.0.1 magic number. Never forget it.**
|
||||
6. `addr.sin_port = htons(atoi(argv[1]));`
|
||||
7. `if (bind(...) < 0) fatal();`
|
||||
8. `if (listen(g_server, 10) < 0) fatal();`
|
||||
|
||||
**Checkpoint B:** compiles. `./mini_serv` → prints `Wrong number of arguments` on stderr, exit 1.
|
||||
`echo $?` shows 1.
|
||||
|
||||
## Stage C — main(), part 2: slots + loop + accept (25 min)
|
||||
|
||||
1. init loop: all `g_c[i].fd = -1; id = -1; buf = NULL;` then `g_maxfd = g_server;`
|
||||
2. `while (1) {` and inside, **in this exact order**:
|
||||
- `FD_ZERO(&readfds);`
|
||||
- `FD_SET(g_server, &readfds);`
|
||||
- loop `i = 0..g_maxfd`: if slot live → `FD_SET(i, &readfds);`
|
||||
- `if (select(g_maxfd + 1, &readfds, NULL, NULL, NULL) < 0) continue ;`
|
||||
- `if (FD_ISSET(g_server, &readfds)) accept_new();`
|
||||
- loop `i = 0..g_maxfd`: if `i != g_server && slot live && FD_ISSET(i, &readfds)` → `handle_read(i);`
|
||||
3. `accept_new()` (write it now, above main):
|
||||
```c
|
||||
fd = accept(g_server, NULL, NULL); if (fd < 0) return ;
|
||||
g_c[fd].fd = fd; g_c[fd].id = g_next_id++; g_c[fd].buf = NULL;
|
||||
if (fd > g_maxfd) g_maxfd = fd;
|
||||
sprintf(msg, "server: client %d just arrived\n", g_c[fd].id);
|
||||
to_all_except(fd, msg); /* NOT to the newcomer */
|
||||
```
|
||||
4. `to_all_except(fd, s)`: loop `i = 0..g_maxfd`, `if (i != fd && g_c[i].fd != -1) send(g_c[i].fd, s, strlen(s), 0);`
|
||||
|
||||
**Checkpoint C:** compiles. Then live test: `./mini_serv 8081` in terminal 1,
|
||||
`nc 127.0.0.1 8081` in terminal 2 (nothing appears), open terminal 3 with `nc 127.0.0.1 8081`
|
||||
→ **terminal 2 must show `server: client 1 just arrived`.**
|
||||
(Client ids: viewer is 0, newcomer is 1 — and the newcomer sees nothing for itself. That's correct!)
|
||||
|
||||
## Stage D — the read path (30 min, the exam lives here)
|
||||
|
||||
1. `handle_read(fd)`:
|
||||
```c
|
||||
n = recv(fd, tmp, sizeof(tmp) - 1, 0);
|
||||
if (n <= 0) { disconnect(fd); return ; }
|
||||
tmp[n] = 0;
|
||||
g_c[fd].buf = str_join(g_c[fd].buf, tmp); /* append FIRST */
|
||||
if (!g_c[fd].buf) fatal();
|
||||
while (extract_message(&g_c[fd].buf, &msg) == 1) /* then pull lines */
|
||||
{
|
||||
out = malloc(strlen(msg) + 64);
|
||||
if (!out) { free(msg); fatal(); }
|
||||
sprintf(out, "client %d: %s", g_c[fd].id, msg);
|
||||
to_all_except(fd, out); /* NOT to self */
|
||||
free(out); free(msg);
|
||||
}
|
||||
```
|
||||
2. `str_join` + `extract_message`: **copy verbatim from `subject/main.c`.** You are allowed to.
|
||||
Type them once by hand tonight (D3), then you'll never think about them again.
|
||||
|
||||
**Checkpoint D:** terminal 3 types `hello` + Enter → **terminal 2 shows `client 1: hello`.**
|
||||
Terminal 3 itself shows nothing. Partial-line test: type `par` Enter? No — type `par` and
|
||||
*don't* press Enter yet; terminal 2 must stay silent until you press Enter.
|
||||
|
||||
## Stage E — disconnect (10 min)
|
||||
|
||||
`disconnect(fd)`: save `id` → `close(fd)` → slot `fd = -1; id = -1` → `free(buf); buf = NULL;`
|
||||
→ `sprintf(msg, "server: client %d just left\n", id);` → `to_all_except(-1, msg);`
|
||||
→ shrink: `while (g_maxfd > g_server && g_c[g_maxfd].fd == -1) g_maxfd--;`
|
||||
|
||||
**Checkpoint E:** Ctrl-D in terminal 3 → terminal 2 shows `server: client 1 just left`.
|
||||
Retype an incomplete line and Ctrl-D → **no message** with it, only `left` (buffer dropped).
|
||||
|
||||
## Stage F — grade yourself (5 min)
|
||||
|
||||
From the repo root: `bash tester/check.sh` → 13/13 + `GRADEME: PASSED`.
|
||||
|
||||
---
|
||||
|
||||
## Pace targets
|
||||
|
||||
| Rep | Goal | Time |
|
||||
|---|---|---|
|
||||
| 1st (today, guided allowed) | compiles, checkpoints C–E work with `nc` | 60–90 min |
|
||||
| 2nd (blank, cheatsheet only on failure) | `bash tester/check.sh` all green | ≤ 60 min |
|
||||
| 3rd (blank, exam conditions) | all green, no references at all | ≤ 45 min |
|
||||
|
||||
**When any stage fails, re-drill *only that stage* 3× before the next full rep.**
|
||||
Partial-line handling (Stage D) is where 80% of candidates fail — over-drill it.
|
||||
Reference in New Issue
Block a user