The TCP communication process is generally the following steps:
The server binds the port and waits for the client to connect.
The client connects to the server through the server's IP and server-bound ports.
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:
Get_host_name Get host Name
Get_ip_address Get IP Address
Get_port_number Get Port number
2. Ace_sock_acceptor class.
Service end use, for binding ports and passively accepting connections.
Common methods:
Open bound port
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:
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:
Send () sends data
Recv () Receive data
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