Ace implements simple TCP Communication

Source: Internet
Author: User

 

From http://andylin02.javaeye.com/blog/470287

Overview:

Transmission Control Protocol TCP (Transmission Control Protocol): TCP provides reliable and connection-oriented transport services for high-reliability data transmission. The reliability of TCP is to ensure that each TCP packet can arrive at the client in the sending order.

The TCP communication process is generally as follows:

  1. The server binds the port and waits for the client to connect.
  2. The client connects to the server through the Server IP address and the port bound to the server.
  3. The server and the client establish a data path through the network to perform data interaction.

Common APIs:

1. ace_inet_addr class.

The sub-class of ACE "Address" ace_addr, indicating the TCP/IP and UDP/IP addresses. It usually contains the IP address and port information of the machine. It can be used to locate the communication process.

Definition method:
Ace_inet_addr addinfo (3000, "192.168.1.100 ");
Common Methods:

  1. Get_host_name
  2. Get_ip_address
  3. Get_port_number

2. ace_sock_acceptor class.

Used by the service end to bind the port and passively accept the connection.
Common Methods:

  1. Open binding port
  2. Accept establishes a connection with the customer segment

3.Ace_sock_connector class.

Used by the client to actively establish connections with the server.
Common Methods:

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

 4. ace_sock_stream class.

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

  1. Send () send data
  2. Recv () receives data
  3. Close () Close the connection (in fact, the socket connection is closed ).

Sample Code:

The following example demonstrates how to use ACE to create a TCP communication server.

# 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_listen (3000); // The bound port.
Ace_sock_acceptor acceptor;
If (acceptor. Open (port_to_listen, 1) =-1) // bind the port
{
Cout <Endl <"bind port fail" <Endl;
Return-1;
}

While (true)
{
Ace_sock_stream peer; // data path of the Client
Ace_time_value timeout (10, 0 );

If (acceptor. Accept (PEER )! =-1) // establish a connection with the client
{
Cout <Endl <"client connect." <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 the data sent by the client
{
Peer. Send (buffer, bytes_received); // send data to the client
}
Peer. Close ();
}
}

Return 0;
}

The function implemented in this example is very simple. The server binds port 3000, waits for a client connection, and then forwards the data read from the client to the client again, that is, an echoserver is implemented.

The corresponding client program is also relatively simple, and the code is as follows:

# 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 (3000, "127.0.0.1 ");

Ace_sock_connector conne;
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 received bytes

Char Buf [1024];
BC = peer. Recv (BUF, 1024, & timeout); // receives data
If (BC> = 0)
{
Buf [BC] = '/0 ';
Cout <Endl <"Rev:/t" <Buf <Endl;
}
Peer. Close ();

Return 0;
}

The following table compares the transmission process between the server and client:

Operation

Client

Server

Initialization

No

Call acceptor. open () to bind the port

Establish a connection

Call connector. Connect ()

Call the acceptor. Accept () method

Transmit data

Send: Call the peer. Recv () method
Receive: Call the peer. Send () method

Close connection

Call the peer. Close () method

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.