Ace Socket Preliminary

Source: Internet
Author: User

The TCP communication process is generally the following steps:

    1. The server binds the port and waits for the client to connect.

    2. The client connects to the server through the server's IP and server-bound ports.

    3. The server and the client establish a data path through the network to interact with the data through this data path.

Common APIs:

1. Ace_inet_addr class.

The subclass of the ACE "Address" class ace_addr, which represents the addresses of TCP/IP and UDP/IP. It usually contains the IP and port information of the machine, through which it can locate the process of communication.

How to define:
Ace_inet_addr Addinfo (3000, "192.168.1.100");
Common methods:

    1. Get_host_name Get host Name

    2. Get_ip_address Get IP Address

    3. Get_port_number Get Port number

2. Ace_sock_acceptor class.

Service end use, for binding ports and passively accepting connections.
Common methods:

    1. Open bound port

    2. Accept to establish a connection with the customer segment

3. Ace_sock_connector class.

Used by the client to actively establish and connect to the server.
Common methods:

    1. Connect () establishes the connection with the service period.

4. Ace_sock_stream class.

Both the client and the server are used to represent the data path between the client segment and the server.
Common methods:

    1. Send () sends data

    2. Recv () Receive data

    3. Close () Closes the connection (in effect, the socket connection is disconnected).

code example:

Server side:

#include "ace/sock_acceptor.h" #include "ace/sock_stream.h" #include "ace/inet_addr.h" #include "ace/os.h" #include <string> #include <iostream>using namespace std;int main (int argc, char *argv[]) {ace_inet_addr Port_to_lis        Ten (3000);    The bound port Ace_sock_acceptor acceptor;        if (Acceptor.open (port_to_listen, 1) = =-1)//bind port {cout<<endl<< "bind port fail" <<endl;    return-1;        } while (true) {Ace_sock_stream peer;        And the client's data path Ace_time_value timeout (10, 0); if (acceptor.accept (PEER)! =-1)//establish and client connection {cout<<endl<<endl<<            "<<endl;            Char buffer[1024];            ssize_t bytes_received;            Ace_inet_addr raddr;            PEER.GET_LOCAL_ADDR (RADDR); cout<<endl<< "Local port\t" <<raddr.get_host_name () << "\ T" <<raddr.get_port_number ()            <<endl;    while (bytes_received =            PEER.RECV (buffer, sizeof (buffer)))! =-1)//Read data sent by the client {peer.send (buffer, bytes    _received);        Send data to Client} peer.close (); }} return 0;  }

The server-side binds Port No. 3000, waits for a client to connect, and then forwards the data read from the client back to the client, which is the implementation of a echoserver function.

The corresponding client program:

#include <ace/SOCK_Stream.h> #include <ace/SOCK_Connector.h> #include <ace/INET_Addr.h> #include <ace/Time_Value.h> #include <string> #include <iostream>using namespace std;int main (int argc, char * Argv[]) {    ace_inet_addr Addr ("127.0.0.1");    Ace_sock_connector Connector;        Ace_time_value timeout (5,0);    Ace_sock_stream peer;    if (Connector.connect (peer,addr,&timeout)! = 0)    {        cout<< "Connection failed!" <<endl;        return 1;    }    cout<< "Conneced!" <<endl;    String s= "Hello World";    Peer.send (S.c_str (), s.length ());    Send data    cout<<endl<< "send:\t" <<s<<endl;    ssize_t bc=0;            Number of bytes received    char buf[1024];    BC=PEER.RECV (buf,1024,&timeout);    Receive Data    if (bc>=0)    {        buf[bc]= ' + ';        cout<<endl<< "Rev:\t" <<buf<<endl;    }    Peer.close ();    

Ace Socket Initial

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.