Ace journey-the first ace communication program daytime

Source: Internet
Author: User
Tags doxygen

Ace journey-the first ace CommunicationProgram

The greatest use of ACE is communication. This is not much to say. It imitates the first instance of the UNP of Steven s-the daytime server and client.

Starting from SVR. cpp on the server,CodeAs follows:

# Include " ACE/inet_addr.h "
# Include " ACE/sock_stream.h "
# Include " ACE/sock_acceptor.h "
# Include " ACE/log_msg.h "
# Include < Time. h >

int ace_tmain ( int , ace_tchar * []) {
ace_inet_addr port_to_listen ( " 50000 " );
ace_sock_acceptor acceptor;

If (acceptor. open (port_to_listen, 1 ) = - 1 )
ace_error_return (lm_error, ace_text ( " % P \ n " ), ace_text ( " acceptor. open " )), 100 );

While(1){
Ace_sock_stream peer;
Ace_inet_addr peer_addr;
Ace_time_value timeout (10,0);

If (Acceptor. Accept (peer, & Peer_addr, null, 1 ) = - 1 ){
If (Ace_ OS: last_error () = Eintr)
Ace_debug (lm_debug, ace_text ( " (% P | % t) interrupted while " ),
Ace_text ( " Waiting for connection \ n " )));
} Else {
Ace_tchar peer_name [maxhostnamelen];
Peer_addr.addr_to_string (peer_name, maxhostnamelen );

Ace_debug (lm_debug, ace_text ("(% P | % t) connectionfrom % s \ n"), Peer_name ));

Time_t ticks=Time (null );
CharTimebuf [128];

Memset (timebuf,0,Sizeof(Timebuf ));
Ace_ OS: snprintf (timebuf,Sizeof(Timebuf ),"%. 24s \ r \ n", Ctime (&Ticks ));

Peer. send_n (timebuf, ace_ OS: strlen (timebuf ));
Peer. Close ();
}
}
Return0;
}

Those who have learned the socket should not have much difficulty. Using the native BSD Socket API to compile the TCP server generally requires socket, bind, listen, accept, and other functions, the data structure such as struct sockaddr_in is obtained. In ace

· Ace'sAce_sock_acceptor: Open completes the sock, bind, and listen actions.

· Ace's ace_sock_acceptor: accept corresponds to the accept function.

· Ace_sock_stream corresponds to the peer FD returned by accept in the native Socket API.

· Ace_inet_addrStruct sockaddr_in and other structures.

You only need to know the basic functions of these classes, what is done, and then through a few small examples, combined with the official doxygen generated documentation (http://www.dre.vanderbilt.edu/Doxygen/5.7.7/html/ace/index.html) to view the method of a class in the end, ace is still very studious.

The client code cli. cpp is as follows:

# Include " ACE/inet_addr.h "
# Include " ACE/sock_stream.h "
# Include " ACE/sock_connector.h "
# Include " ACE/log_msg.h "

int ace_tmain ( int , ace_tchar * []) {
ace_inet_addr srvr ( 50000 , ace_localhost);
ace_sock_connector ctor;
ace_sock_stream peer;

If(Connector. Connect (peer, srvr)=-1)
Ace_error_return (lm_error, ace_text ("% P \ n"), Ace_text ("Connect")),1);

IntBC;
CharBuf [64];

BC=Peer. Recv (BUF,Sizeof(BUF ));
Write (1, Buf, BC );
Peer. Close ();

Return0;
}

·Ace_sock_connector: The Connect Method completes the socket, connect, and other functions required by the native Socket API to create a tcp client.

The makefile for compiling these two programs is as follows:

Cc = g ++
Incl = $ (ace_root)/include
Libs = $ (ace_root)/lib

Cli_deps = cli. cpp
Svr_deps = SVR. cpp

. Suffixes:. cpp. o
. Cpp. O:
$ (CC)-g-I $ (incl)-C $ *. cpp

ALL: CLI SVR

CLI: $ (cli_deps)
$ (CC)-g-I $ (incl)-L $ (libs)-O CLI $ (cli_deps)-Restart
SVR: $ (svr_deps)
$ (CC)-g-I $ (incl)-L $ (libs)-O SVR $ (svr_deps)-Regression

Clean:
-RM *~
-RM CLI
-RM SVR

The first example of the socket communication program using ace, of course, Ace is far more than the encapsulation of these socket functions. Next we will use the ace reactor framework to implement this daytime program.

(Indicate the author and source when reprinting. Do not use it for commercial purposes without permission)
MoreArticleVisit my blog: http://www.cnblogs.com/logicbaby


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.