#include <iostream>
#include <stdio.h>
#include <time.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <string>
#include <unistd.h>
#define uchar unsigned char
#define uint unsigned int
using namespace std ;
using boost::asio::ip::udp;
using boost::asio::ip::tcp;
using namespace boost::asio;
class Async_Client_c
{
public:
Async_Client_c(boost::asio::io_service& io_service,ip::tcp::endpoint endpoint)
:iosev(io_service),
socket(iosev),
ep(endpoint)
{
socket.connect(ep,ec);
if(ec)
{
std::cout << boost::system::system_error(ec).what() << std::endl;
}
else
{
cout<<" Connection Success! "<<endl;
}
client_async_read();
}
void client_async_write(char *buf,int len)
{
boost::asio::async_write(socket,
boost::asio::buffer(buf, len),
boost::bind(&Async_Client_c::handle_write, this,
boost::asio::placeholders::error));
}
void client_async_read()
{
socket.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&Async_Client_c::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
printf("Received data Len:%d\n",bytes_transferred);
socket.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&Async_Client_c::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
delete this;
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
}
else
{
delete this;
}
}
io_service &iosev;
ip::tcp::socket socket ;
ip::tcp::endpoint ep ;
boost::system::error_code ec;
enum { max_length = 1024 };
char data_[max_length];
};
int main()
{
io_service iosev;
ip::tcp::endpoint ep(ip::address_v4::from_string("192.168.4.236"),8002);
Async_Client_c tcp_client(iosev,ep) ;
iosev.run();
return 1 ;
}
打印出数据节点:
printf("From %s Received data Len:%d\n",this->ep.address().to_string().c_str(),
bytes_transferred);
把上面的异步tcp 类封装了下,好用多了,加入了 读取回调,断线防呆,这些功能,可以直接使用:
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <string>
#include <unistd.h>
#include <boost_tcpclient.h>
using namespace std ;
using boost::asio::ip::udp;
using boost::asio::ip::tcp;
using namespace boost::asio;
typedef void (*fundata_t)(const char* addr,int port,const char* data,const int len);
class Async_Client_c
{
public:
Async_Client_c(boost::asio::io_service& io_service,ip::tcp::endpoint endpoint,fundata_t fundata_ )
:iosev(io_service),
socket(iosev),
ep(endpoint),
m_connected(0),
m_fundata(fundata_)
{
socket.connect(ep,ec);
if(ec)
{
std::cout << boost::system::system_error(ec).what() << std::endl;
m_connected = 0 ;
}
else
{
cout<<" Connection Success! "<<ep.address().to_string()<<endl;
m_connected = 1 ;
}
client_async_read();
}
void client_async_write(char *buf,int len)
{
boost::asio::async_write(socket,
boost::asio::buffer(buf, len),
boost::bind(&Async_Client_c::handle_write, this,
boost::asio::placeholders::error));
}
void client_async_read()
{
socket.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&Async_Client_c::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
bool client_return_status(){
return m_connected ;
}
private:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
printf("From %s Received data Len:%d\n",this->ep.address().to_string().c_str(),
bytes_transferred);
(*m_fundata)(this->ep.address().to_string().c_str(),this->ep.port(),data_,bytes_transferred);
this->client_async_read();
}
else
{
printf("%s\n",error.message().c_str());
m_connected = 0 ;
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
}
else
{
printf("%s\n",error.message().c_str());
m_connected = 0 ;
}
}
io_service &iosev;
ip::tcp::socket socket ;
ip::tcp::endpoint ep ;
boost::system::error_code ec;
char data_[max_length];
bool m_connected ;
fundata_t m_fundata ;
};
int InitTcpConnection(const char* addr,int port,Async_Client_c **client_,fundata_t fundata_)
{
io_service iosev;
ip::tcp::endpoint ep(ip::address_v4::from_string(addr),port);
*client_ = new Async_Client_c(iosev,ep,fundata_);
iosev.run();
return 1 ;
}
int boost_tcp_init_connection(const char* addr,int port,Async_Client_c **client_,fundata_t fundata_)
{
int timecnt=0 ;
*client_ = NULL ;
boost::thread tmp(&InitTcpConnection,addr,port,client_,fundata_);
tmp.detach() ;
while(timecnt<50){
timecnt++ ;
usleep(20000); //20 ms
if((*client_)->client_return_status()){
return 0 ;
}
}
*client_ = NULL ;
return -1 ;
}
int boost_tcp_sync_send(Async_Client_c *client_ ,const char* msg,const int len)
{
if(client_==NULL || client_->client_return_status()==0 ){
printf("not connected , please connect first \n");
return -1 ;
}
else{
client_->client_async_write((char*)msg,len);
return 0 ;
}
return 1;
}
int boost_tcp_sync_read(Async_Client_c *client_ )
{
if(client_==NULL || client_->client_return_status()==0 ){
printf("not connected , please connect first \n");
return -1 ;
}
else{
client_->client_async_read();
return 0 ;
}
return 1;
}
/* ------------------------------------------------------------------------------------------
* show demo --
* ------------------------------------------------------------------------------------------ */
void CallBackRead(const char* addr,int port,const char* data,const int len)
{
printf("call back demo addr:%s port:%d len: %d \n",addr,port,data,len);
}
int main()
{
printf("boost tcp test \n");
char strtest[] = {0xa1,0x44,0x45,0x46,0x47,0x48,0x49,0x4a};
Async_Client_c *client=NULL;
boost_tcp_init_connection("172.16.1.171",5000,&client,&CallBackRead);
boost_tcp_sync_send(client,strtest,8);
boost_tcp_sync_read(client);
sleep(5);
boost_tcp_sync_send(client,strtest,8);
while(1);
return 0 ;
}
#ifndef BOOST_TCPCLIENT_H_
#define BOOST_TCPCLIENT_H_
#define max_length 1024
class Async_Client_c ;
int boost_tcp_sync_send(Async_Client_c *client_ ,const char* msg,const int len);
int boost_tcp_sync_read(Async_Client_c *client_ );
#endif /* BOOST_TCPCLIENT_H_ */