首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

Muduo 移植 Twisted Finger 服務器教程(finger01-07)

     編輯:關於C++

漸進式實現

步驟功能關鍵代碼
01空 EventLoop,拒絕所有連接loop.loop();
02Listen 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

Telnet 測試

$ ./twisted_finger07
$ telnet localhost 1079
muduo
No such user
Connection closed.

$ telnet localhost 1079
schen
Happy and well
Connection closed.

代碼見 examples/twisted/finger。安全問題(緩衝區無上限、線性查找)留待後續改進。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved