Muduo network programming example 4: Twisted Finger

Source: Internet
Author: User

1. Connection denied
Do nothing, and the program is empty. Finger01.cc

1: # include 2: 3: using namespace muduo; 4: using namespace muduo: net; 5: 6: int main () 7: {8: EventLoop loop; 9: loop. loop (); 10 :}
2. accept new connections
Listen for new connections on port 1079, do nothing after accepting connections, and leave the program empty. Muduo automatically discards the received data. Finger02.cc

1: # include 2: # include 3: 4: using namespace muduo; 5: using namespace muduo: net; 6: 7: int main () 8: {9: eventLoop loop; 10: TcpServer server (& loop, InetAddress (1079), "Finger"); 11: server. start (); 12: loop. loop (); 13 :}
3 actively disconnect
Take the initiative to disconnect after accepting the new connection. Finger03.cc

The header file and namespace are omitted below.

1: void onConnection (const TcpConnectionPtr & conn) 2: {3: if (conn-> connected () 4: {5: conn-> shutdown (); 6 :} 7:} 8: 9: int main () 10: {11: EventLoop loop; 12: TcpServer (& loop, InetAddress (1079), "Finger"); 13: server. setConnectionCallback (onConnection); 14: server. start (); 15: loop. loop (); 16 :}
4. Read the user name and disconnect
If you read a message ending with one line, the connection is closed. Finger04.cc

Note that this code has security issues. If malicious clients continuously send data without line breaks, the server's memory will pop up. In addition, Buffer: findCRLF () is a linear query. If the client sends a byte each time, the time complexity of the server is O (N ^ 2), which consumes CPU resources.

1: void onMessage (const TcpConnectionPtr & conn, 2: Buffer * buf, 3: Timestamp receiveTime) 4: {5: if (buf-> findCRLF () 6: {7: conn-> shutdown (); 8:} 9:} 10: 11: int main () 12: {13: EventLoop loop; 14: TcpServer server (& loop, inetAddress (1079), "Finger"); 15: server. setMessageCallback (onMessage); 16: server. start (); 17: loop. loop (); 18 :}
5. Read the user name, output error information, and disconnect
If you read a message ending with one line, an error message is sent and the connection is closed. Finger05.cc

Security issues are the same as above.

1: void onMessage (const TcpConnectionPtr & conn, 2: Buffer * buf, 3: Timestamp receiveTime) 4: {5: if (buf-> findCRLF () 6: {7: conn-> send ("No such user"); 8: conn-> shutdown (); 9:} 10:} 11: 12: int main () 13: {14: eventLoop loop; 15: TcpServer server (& loop, InetAddress (1079), "Finger"); 16: server. setMessageCallback (onMessage); 17: server. start (); 18: loop. loop (); 19 :}
6. Search for users from the empty UserMap
Get the username (row 22nd) from a message line, search in UserMap, and return the result. Finger06.cc

Security issues are the same as above.

1: typedef std: map UserMap; 2: UserMap users; 3: 4: string getUser (const string & user) 5: {6: string result = "No such user "; 7: UserMap: iterator it = users. find (user); 8: if (it! = Users. end () 9: {10: result = it-> second; 11:} 12: return result; 13:} 14: 15: void onMessage (const TcpConnectionPtr & conn, 16: Buffer * buf, 17: Timestamp receiveTime) 18: {19: const char * crlf = buf-> findCRLF (); 20: if (crlf) 21: {22: string user (buf-> peek (), crlf); 23: conn-> send (getUser (user) + ""); 24: buf-> retrieveUntil (crlf + 2); 25: conn-> shutdown (); 26:} 27:} 28: 29: int main () 30: {31: eventLoop loop; 32: TcpServer server (& loop, InetAddress (1079), "Finger"); 33: server. setMessageCallback (onMessage); 34: server. start (); 35: loop. loop (); 36 :}
7. Add a user to UserMap
It is almost identical to the previous one, with only 31st rows added. Finger07.cc

1: typedef std: map UserMap; 2: UserMap users; 3: 4: string getUser (const string & user) 5: {6: string result = "No such user "; 7: UserMap: iterator it = users. find (user); 8: if (it! = Users. end () 9: {10: result = it-> second; 11:} 12: return result; 13:} 14: 15: void onMessage (const TcpConnectionPtr & conn, 16: Buffer * buf, 17: Timestamp receiveTime) 18: {19: const char * crlf = buf-> findCRLF (); 20: if (crlf) 21: {22: string user (buf-> peek (), crlf); 23: conn-> send (getUser (user) + ""); 24: buf-> retrieveUntil (crlf + 2); 25: conn-> shutdown (); 26:} 27:} 28: 29: int main () 30: {31: users ["schen"] = "Happy and well"; 32: EventLoop loop; 33: TcpServer (& loop, InetAddress (1079), "Finger"); 34: server. setMessageCallback (onMessage); 35: server. start (); 36: loop. loop (); 37 :}
The above is all content. You can use telnet as the client to test our simple finger server.

Telnet Test
Run in a command line window

$./Bin/twisted_finger07

Another command line

$ Telnet local host 1079
Trying: 1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is ^].
Muduo
No such user
Connection closed by foreign host.

Try again

$ Telnet local host 1079
Trying: 1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is ^].
Schen
Happy and well
Connection closed by foreign host.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.