2.8 KiB
2.8 KiB
EXAM CARD — mini_serv (one screen, read right before you start)
Exact strings (bytes matter, \n included)
write(2, "Wrong number of arguments\n", 26); return 1; /* argc != 2 */
write(2, "Fatal error\n", 12); exit(1); /* syscall/alloc failed */
sprintf(m, "server: client %d just arrived\n", id); /* to OTHERS */
sprintf(o, "client %d: %s", id, line); /* per line, OTHERS */
sprintf(m, "server: client %d just left\n", id); /* to everyone left */
127.0.0.1 ⇔ htonl(2130706433) · only localhost · no #define · no printf in the file.
The 8 moves
argc != 2→ args error · 2.socket(AF_INET, SOCK_STREAM, 0)→fatal()·sockaddr_in: bzero,AF_INET,htonl(2130706433),htons(atoi(argv[1]))→bind→listen·- slots init
fd/id/buf = -1,-1,NULL,g_maxfd = g_server· - every loop:
FD_ZERO+FD_SET(g_server)+FD_SETlive fds →select(g_maxfd + 1, NULL, NULL, NULL)· - listener ready →
accept→id = g_next_id++→ raiseg_maxfd→ "arrived" to others · - client ready →
recv→n<=0disconnect / else appendstr_join,while extract_message→sprintf("client %d: %s")→ send to others →free(msg)· - disconnect → keep id,
close, clear slot,free(buf), "left" to everyone, shrinkg_maxfd(≥ server fd).
The 5 traps that fail exams
- Not rebuilding/copying the fd_set every loop (or wrong
nfds) → newest client invisible / 100% CPU. - Sending arrival or messages to the sender/newcomer → extra lines → diff fails.
- Flushing the partial buffer at disconnect → phantom message → diff fails.
- One line per
recvinstead of awhileextraction loop → lines lost/delayed. - Closing an fd still in the set →
selectfailsEBADF; alwaysFD_CLR/remove logically beforeclose.
My commands
bash tester/check.sh # compile + 13 behavioral + real grademe diff
python3 tester/mini_serv_tester.py sandbox/mini_serv --quick # fast loop
cd sandbox && bash .system/grading/tester.sh ; ls .system/grading/passed || sed -n '1,40p' traceback
Exam-day order (first 10 minutes)
- Read subject twice; copy the exact strings from it, not from memory.
- Open the
main.cattachment; plan to copystr_join+extract_messageverbatim. - Type Stage A→E from
drills/typing-script.md; test withncafter C and after D. ncsanity: 2 windows — arrival, relay, split line, Ctrl-C → left.- Re-read the subject; submit; fix; resubmit if allowed. Never sit on a working copy.
If stuck (first three checks)
ss -tlnp→ LISTEN? 2.ncconnects? 3. trace prints: which fd got ready,recvn, buffer contents. Symptom table:debugging.md§5. Exit codes: 141 = SIGPIPE, 139 = segv, 1 = yourexit(1).