Muduo network programming example: Five Simple TCP Protocols

Source: Internet
Author: User

Discard
Discard is probably the simplest persistent connection TCP application layer protocol. It only needs to pay attention to the "message/Data arrival" event in the "Three-half events". The event processing function is as follows:

1: void DiscardServer: onMessage (const muduo: net: TcpConnectionPtr & conn,

2: muduo: net: Buffer * buf,

3: muduo: Timestamp time)

4 :{

5: string msg (buf-> retrieveAsString (); // retrieve all read data

6: LOG_INFO <conn-> name () <"discards" <msg. size () <"bytes at" <time. toString ();

7:} the rest is the routine code: Define a DiscardServer class, with TcpServer as a member. 1: # ifndef MUDUO_EXAMPLES_SIMPLE_DISCARD_DISCARD_H 2: # define MUDUO_EXAMPLES_SIMPLE_DISCARD_DISCARD_H 3: 4: # include 5: 6: // RFC 863 7: class DiscardServer

8 :{

9: public:

10: DiscardServer (muduo: net: EventLoop * loop,

11: const muduo: net: InetAddress & listenAddr );

12:

13: void start ();

14:

15: private:

16: void onConnection (const muduo: net: TcpConnectionPtr & conn );

17:

18: void onMessage (const muduo: net: TcpConnectionPtr & conn,

19: muduo: net: Buffer * buf,

20: muduo: Timestamp time );

21:

22: muduo: net: EventLoop * loop _;

23: muduo: net: TcpServer server _;

24 :};

25:

26: # endif // MUDUO_EXAMPLES_SIMPLE_DISCARD_DISCARD_H registers the callback function

1: DiscardServer (muduo: net: EventLoop * loop,

2: const muduo: net: InetAddress & listenAddr)

3: loop _ (loop ),

4: server _ (loop, listenAddr, "DiscardServer ")

5 :{

6: server _. setConnectionCallback (

7: boost: bind (& DiscardServer: onConnection, this, _ 1 ));

8: server _. setMessageCallback (

9: boost: bind (& DiscardServer: onMessage, this, _ 1, _ 2, _ 3 ));

10 :}

11:

12: void DiscardServer: start ()

13 :{

14: server _. start ();

15 :}

Handling connection and Data Events

1: void DiscardServer: onConnection (const muduo: net: TcpConnectionPtr & conn)

2 :{

3: LOG_INFO <"DiscardServer-" <conn-> peerAddress (). toHostPort () <"->"

4: <conn-> localAddress (). toHostPort () <"is"

5: <(conn-> connected ()? "UP": "DOWN ");

6 :}

7:

8: void DiscardServer: onMessage (const muduo: net: TcpConnectionPtr & conn,

9: muduo: net: Buffer * buf,

10: muduo: Timestamp time)

11 :{

12: string msg (buf-> retrieveAsString ());

13: LOG_INFO <conn-> name () <"discards" <msg. size () <"bytes at" <time. toString ();

14 :}

Use EventLoop in main () to turn the entire program

1: # include "discard. h"

2:

3: # include

4: # include

5:

6: using namespace muduo;

7: using namespace muduo: net;

8:

9: int main ()

10 :{

11: LOG_INFO <"pid =" <getpid ();

12: EventLoop loop;

13: InetAddress listenAddr (2009 );

14: DiscardServer server (& loop, listenAddr );

15: server. start ();

16: loop. loop ();

17:} daytime
Daytime is a short connection protocol. After the current time is sent, the server proactively disconnects the connection. It only needs to pay attention to the "Connection established" event in the "Three and a half events". The event processing function is as follows:

1: void DaytimeServer: onConnection (const muduo: net: TcpConnectionPtr & conn)

2 :{

3: LOG_INFO <"DaytimeServer-" <conn-> peerAddress (). toHostPort () <"->"

4: <conn-> localAddress (). toHostPort () <"is"

5: <(conn-> connected ()? "UP": "DOWN ");

6: if (conn-> connected () 7: {8: conn-> send (Timestamp: now (). toFormattedString () + ""); // sending time string 9: conn-> shutdown (); // actively disconnect 10:} 11 :} the rest are routine code. To save space, read muduo/examples/simple/daytime.

Use netcat to play the client. The running result is as follows:

$ Nc 127.0.0.1 2013
03:31:26. 622647 # Time string returned by the server

Time
The Time protocol is very similar to daytime, except that it returns a 32-bit integer instead of a date and Time string, indicating the number of seconds from 00: 00: 00Z to the present. Of course, this agreement has a 2038 issue ". The server only needs to pay attention to the "Connection established" event in "three and a half events". The event processing function is as follows:

1: void TimeServer: onConnection (const muduo: net: TcpConnectionPtr & conn) 2: {3: LOG_INFO <"TimeServer-" <conn-> peerAddress (). toHostPort () <"->" 4: <conn-> localAddress (). toHostPort () <"is" 5: <(conn-> connected ()? "UP": "DOWN"); 6: if (conn-> connected () 7: {8: int32_t now = sockets: hostToNetwork32 (static_cast <int> (:: time (NULL); 9: conn-> send (& now, sizeof now); // send 4 bytes 10: conn-> shutdown (); // actively disconnect the connection 11 :}12 :}. The rest are routine code. To save space, read muduo/examples/simple/time here.

Use netcat

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.