Ace windows Configuration

Source: Internet
Author: User

1. Compile, download the source code, and decompress the package directly to drive C.

2. find ace in ace_wrapper, which has a version of vc8.0. You need to create a new config before directly compiling. h, then add the following content to this header file: # define ace_has_standard_cpp_library 1 # include "ACE/config-win32.h"

3. CreateProgramRemember to set the configuration properties of the program-connector-input-additional dependencies, and enter the following content: C: \ ace_wrappers \ Lib \ ACE. Lib

4. in this step, you can use ACE Lib, but to further use its functions, You need to perform the following operations: tool -- option -- project and solution --- VC ++ directory -- contains file input: C: \ ace_wrappers 5. now you can successfully compile the ace example.

Over

 

For example:

 

To use ACE for socket programming, you need to use the following classes:
Ace_sock_connector: Connector that actively establishes a connection for Socket Client;
Ace_sock_acceptor: receiver, used to passively establish a connection for socket server;
Ace_sock_stream: the stream for data transmission;
Ace_inet_addr: used to indicate the address of the Communication endpoint;
The ACE/inet_addr.h file defines some useful ace_inet_addr constructor for creating the address of the Communication endpoint. Once the ace_inet_addr information of a communication endpoint is constructed, you can use this address to connect to the server. Ace uses the ace_sock_stream class object to indicate that the TCP socket has been successfully connected, because TCP connections represent connection-oriented virtual connections or "byte streams ";
Short writing : When you try to write some bytes to the remote host, your bytes are not all sent due to network buffer overflow, congestion, or any other reason. Then, you must move your data pointer to send the remaining data. You must continuously send the data until all the original data is sent out. this problem occurs frequently in network programming. Ace's send_n () method calls encapsulate these operations, and it will convert all these retry operations into its own internal transactions, in this way, only the specified bytes are sent completely, or an error occurs when the specified bytes are sent;
Short read Problems : When you try to receive some data from a remote host, you cannot receive all the data at a time due to network congestion, latency, and other reasons, you must calculate the number of bytes of received data to receive the remaining data. The Recv () method reads data of up to n Bytes from the peer end, and put the N pieces of data in the receiving buffer; of course, if you know exactly the number of bytes of the data to be received, you must deal with the "Short read" problem; ACE provides the recv_n () method call to solve the "Short read" problem. It is the same as the send_n () method. You must tell it the exact number of bytes to read, it receives the data of all specified bytes before the call returns;
Ace_inet_addr: Set (): This method is flexible. You can use it to modify the attributes of an address object. In this way, you can reuse an address object without creating multiple address objects; it is as flexible as the constructor of ace_inet_addr. When set () fails to be called, the Set () method returns-1. You can use ace_ OS: last_error () to check the error code; in Unix or Unix-like systems, ace_ OS: last_error () simply returns the errno value. However, in Windows, it calls the getlasterror () function to return the error code;
Ace_sock_connector: connet (): actively connects to the server. If the connection fails,-1 is returned. If the connection succeeds, 0 is returned. In most cases, the operating system selects the local port for you, ace uses ace_addr: sap_any to represent this value. However, in many cases, you may want to select the local port value, when we actively connect to the server as a customer, we can select the local port used by the customer. This is not very safe to protect the application, but can play a role in preventing spoofing; we can set service quality parameters on our connections, or even start blocking and non-blocking connection operations; for operations above the socket, they support setting timeout for long-running operations; like the Connect Method of ace_sock_connector, we need to provide an ace_time_value Class Object Based on the required timeout time, such as send_n () and recv_n () and other operations can receive an ace_time_value object as the timeout parameter;
Differences between readv () and writev () and read () and write () :
The functions of readv () and writev () are the same as those of read () and write (). They are both system calls and read/write system calls of Io, but read () and write () it must be used for consecutive data areas, while readv () and writev () can be used for Discontinuous data areas or data blocks. You can use arrays of iovec struct to define discontinuous data areas; readv (), writev (), and structured iovec are introduced in the bsd4.3 operating system. They are most commonly used when data is received and sent using discontinuous buffers; A common example is to send a packet header and data associated with the packet header that is stored in another buffer; if you use a standard system to call write, you must call write () twice in a row to send headers and bodies respectively. This operation is troublesome and the efficiency cannot be kept up; if you can write your head and body together in an atomic way, or you need to avoid Nagle Algorithm It is unacceptable to make two calls. If you copy your head and body to a larger buffer, This is not realistic in terms of memory requirements; in this case, the system call writev () and the array of the iovec struct can be used to solve this problem. writev () and ace_sock_stream: sendv () the two methods will send all entries in the array of the iovec struct in an atomic way, while readv () and ace_sock_stream: recvv () are the same as writev () and ace_sock_stream:: sendv (), on the contrary, the two are to fill the received data in sequence into the discontinuous buffer zone marked by each entry in the array of the iovec struct, then write the pointer to the starting position of the next discontinuous buffer;
Typedef char * caddr_t ;/*? <Core address> type */
Struct iovec
{
Int iov_len;
Caddr_t iov_base;
};
The iov_base member can point to the memory ing file area, shared memory segment, or somewhere else. You can specify the receiving buffer address by yourself, or let the recvv () method automatically allocate the receiving buffer for you, fill the iovec structure with the pointer and buffer length. The recvv () method calculates how much data is to be received and allocates a buffer with the same size as the data to be received; if you do not know how much data the other party has to send to you, but you are quite clear that all the data can be put into a reasonable size space, and you want this space to be continuous, this function can be used. However, it must be noted that the recvv () method helps you allocate the receiving buffer. Therefore, after you use these buffers, remember to release the memory to avoid Memory leakage;
Ace_const_cast (type, variable );
Ace_static_cast (type, variable );
Ace_reinterpret_cast (type, variable );
All three functions perform variable type conversion. They convert the variable type to the type;
Build a server:
To create a server, you must first create an ace_inet_addr class object to define the port you want to use to listen for connection requests. then, you need to use an ace_sock_acceptor object to open a listener on this port. The ace_sock_acceptor: accept () method takes care of lower-layer details, including BIND (), listen () and accept (). If the accept () method is successfully called, a valid peer object that has been initialized is returned through the accept () parameter, it represents the connection to communicate with the client;
It is worth mentioning that, by default, if the accept () method is interrupted by a Unix signal, it will restart itself. this may be appropriate or inappropriate for your application. We can use the fourth parameter of the accept () method to specify whether to restart accept () method itself. If the value of this parameter is 0, it indicates that when the accept () method is interrupted by the signal, the accept () method does not need to restart itself, but returns-1, indicates an error. If the value of this parameter is 1, it indicates that after the accept () method is interrupted by the signal, the accept () method needs to restart itself;
If the accept () method is successfully called and a client connection is returned, it also fills in the address information of the connected client to an ace_inet_addr object; ace_inet_addr:: addr_to_string (), which is used to convert an IP address to a character representation;

Client exampleCode:
# Include "ACE/log_msg.h"
# Include "ACE/inet_addr.h"
# Include "ACE/sock_stream.h"
# Include "ACE/sock_connector.h"
# Include "ACE/sstring. H"

Int ace_tmain (INT argc, ace_tchar ** argv)
{
Ace_inet_addr SVR (5000, ace_localhost );
Ace_sock_stream peer;
Ace_sock_connector conne;
Ace_tchar strbuffer [1024];
Ace_sstring strsend;
Ssize_t bytes_received = 0;

If (connector. Connect (peer, SVR) =-1)
{
Ace_debug (lm_error, ace_text ("--> % p | % t connect failed \ n"), ace_text ("Connect ")));
Return-1;
}

Ace_debug (lm_info, ace_text ("Connect server OK \ n ")));

Strsend = "Hello, ACE server ";
Peer. send_n (strsend. c_str (), strsend. Length ());

Ace_ OS: memset (strbuffer, 0, sizeof (strbuffer ));
Bytes_received = peer. Recv (strbuffer, sizeof (strbuffer ));

Ace_debug (lm_info, ace_text ("receive from server: % s \ n"), strbuffer ));

Peer. Close ();
Return 0;
}

Server example code:
# Include "ACE/log_msg.h"
# Include "ACE/inet_addr.h"
# Include "ACE/sock_stream.h"
# Include "ACE/sock_acceptor.h"
# Include "ACE/sstring. H"

Int ace_tmain (INT argc, ace_tchar ** argv)
{
Ace_inet_addr port_to_listen (5000, ace_localhost), client_addr;
Ace_sock_acceptor acceptor;
Ace_sock_stream client;
Ace_tchar strclientaddr [64], strbuffer [1024];
Ssize_t bytes_received = 0;

If (acceptor. Open (port_to_listen, 1) =-1)
{
Ace_debug (lm_error, ace_text ("---> % P \ n"), ace_text ("acceptor. Open ")));
Return-100;
}

While (1)
{
If (acceptor. Accept (client, & client_addr) =-1)
{
Ace_debug (lm_error, ace_text ("(% p | % t) failed to accept \ n"), ace_text ("Client Connection ")));
Acceptor. Close ();
Break;
}

Client_addr.addr_to_string (strclientaddr, sizeof (strclientaddr ));
Ace_debug (lm_info, ace_text ("client % s connected \ n"), strclientaddr ));
Ace_ OS: memset (strbuffer, 0, sizeof (strbuffer ));
If (bytes_received = client. Recv (strbuffer, sizeof (strbuffer) =-1)
{
Ace_debug (lm_error, ace_text ("% p | % t encoded ed failed \ n"), ace_text ("cleint. Recv ")));
Client. Close ();
Break;
}

Ace_debug (lm_info, ace_text ("receive from client: [% d] % s \ n"), bytes_received, strbuffer ));

Client. send_n (strbuffer, ace_ OS: strlen (strbuffer ));

Client. Close ();
}

Acceptor. Close ();
Return 0;
}

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.