同步tcp服务端
同步tcp客户端
异步tcp服务端
基于流的tcp服务端
基于流的tcp客户端
udp服务端
udp客户端
#include <iostream>
#include <boost/asio.hpp>
using namespace boost;
using namespace boost::asio;
int main()
{
try {
io_service ios;
ip::tcp::acceptor acceptor(ios,ip::tcp::endpoint(ip::tcp::v4(),6688));
std::cout<<acceptor.local_endpoint().address()<<std::endl;
while(true)
{
ip::tcp::socket sock(ios);
acceptor.accept(sock);
std::cout<<"client:"<<sock.remote_endpoint().address()<<std::endl;
sock.write_some(buffer("hello world\n"));
}
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
同步tcp客户端
#include <iostream>
#include <boost/asio.hpp>
#include <vector>
using namespace boost;
using namespace boost::asio;
int main()
{
try {
io_service ios;
ip::tcp::socket sock(ios);
ip::tcp::endpoint ep(ip::address::from_string("192.168.1.113"),6688);
sock.connect(ep);
std::vector<char> str(100,0);
sock.read_some(buffer(str));
std::cout<<"rec from:"<<sock.remote_endpoint().address()<<std::endl;
std::cout<<&str[0]<<std::endl;
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
异步tcp服务端
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
//using namespace std;
using namespace boost::asio;
class server
{
private:
io_service &ios;
ip::tcp::acceptor acceptor;
typedef boost::shared_ptr<ip::tcp::socket> sock_pt;//定义了一个智能指针,指向socket对象,用来在回调函数中传递
public:
server(io_service& io):ios(io),acceptor(ios,ip::tcp::endpoint(ip::tcp::v4(),6688))
{start();}
//启动异步接受连接,调用accepter的async_accept()函数
void start()
{
sock_pt sock(new ip::tcp::socket(ios));
acceptor.async_accept(*sock,bind(&server::accept_handler,this,placeholders::error,sock));
}
//发送数据
void accept_handler(const boost::system::error_code & ec,sock_pt sock)
{
if(ec)
{
return;
}
std::cout<<"client:";
std::cout<<sock->remote_endpoint().address()<<std::endl;
sock->async_write_some(buffer("hello asio"),bind(&server::write_handler,this,placeholders::error));//异步发送数据,write_handler()异步调用函数
start();//发送完数据后,调用start()再次启动服务接受连接,否则完成数据发送后,io_service因没事件处理而结束运行。
}
//因为我们不需要再做更多的工作,可以直接实现一个空函数,这里输出一条消息
void write_handler(const boost::system::error_code &)
{
std::cout<<"send msg complete."<<std::endl;
}
};
int main()
try
{
std::cout<<"server start."<<std::endl;
io_service ios; //io_service对象
server serv(ios);
ios.run();
}
catch(std::exception& e)
{
std::cout<<e.what()<<std::endl;
}
基于流的tcp服务端
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
using namespace boost::asio;;
int main()
{
try{
io_service ios;
ip::tcp::endpoint ep(ip::tcp::v4(),6688);
ip::tcp::acceptor acceptor(ios,ep);
while(true)
{
ip::tcp::iostream tcp_stream;
acceptor.accept(*tcp_stream.rdbuf());
tcp_stream<<"hello tcp stream";
}
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
}
基于流的tcp客户端
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
using namespace boost::asio;;
int main()
{
try{
ip::tcp::iostream tcp_stream("127.0.0.1","6688");
std::string str;
getline(tcp_stream,str);
std::cout<<str<<std::endl;
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
}
udp服务端
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
//using namespace std;
using namespace boost;
using namespace boost::asio;;
int main()
{
try{
std::cout<<"udp server start"<<std::endl;
io_service ios;
ip::udp::socket sock(ios,ip::udp::endpoint(ip::udp::v4(),6699));
while(1)
{
char buf[1];
ip::udp::endpoint ep;
system::error_code ec;
sock.receive_from(buffer(buf),ep,0,ec);
if(ec && ec != error::message_size)
{
throw system::system_error(ec);
}
std::cout<<"send to "<< ep.address()<<std::endl;
sock.send_to(buffer("hello asio udp"),ep);
}
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
}
udp客户端
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace boost;
using namespace boost::asio;;
int main()
{
try{
std::cout<<"udp client start"<<std::endl;
io_service ios;
ip::udp::endpoint send_ep(ip::address::from_string("127.0.0.1"),6699);
//ip::udp::socket sock(ios,ip::udp::endpoint(ip::udp::v4(),6699));
ip::udp::socket sock(ios);
sock.open(ip::udp::v4());
char buf[2]={"h"};
sock.send_to(buffer(buf),send_ep);
std::vector<char> v(100,0);
ip::udp::endpoint recv_ep;
sock.receive_from(buffer(v),recv_ep);
std::cout<<"recv from:"<<recv_ep.address()<<" ";
std::cout<<&v[0]<<std::endl;
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
}