| 步驟 | 功能 | 關鍵代碼 |
|---|---|---|
| 01 | 空 EventLoop,拒絕所有連接 | loop.loop(); |
| 02 | Listen 1079,接受連接不處理 | TcpServer server(...); server.start(); |
| 03 | 連接建立即 shutdown() | onConnection: if(connected) conn->shutdown(); |
| 04 | 讀到 CRLF 才斷開(有安全隱患:惡意無換行撐爆內存,findCRLF O(N²)) | if(buf->findCRLF()) conn->shutdown(); |
| 05 | 回覆 "No such user" 再斷開 | conn->send("No such user\r\n"); conn->shutdown(); |
| 06 | 從空 UserMap 查找用戶名 | string user(buf->peek(), crlf); conn->send(getUser(user)+"\r\n"); |
| 07 | 預置用戶 users["schen"] = "Happy and well" | main() 裡初始化 map |
$ ./twisted_finger07
$ telnet localhost 1079
muduo
No such user
Connection closed.
$ telnet localhost 1079
schen
Happy and well
Connection closed.
代碼見 examples/twisted/finger。安全問題(緩衝區無上限、線性查找)留待後續改進。