#include<stdio.h> #include<boost\asio.hpp> #include<iostream> #include<stdlib.h> int main() { boost::asio::io_context service; boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 2001); //boost::system::error_code e; boost::asio::ip::tcp::socket sock(service); try{ sock.connect(ep); char datasend[512]; char datarecv[512]; memset(datarecv, '\0', 512); memset(datasend, '\0', 512); strcpy(datasend, "datatttttttt"); sock.send(boost::asio::buffer(datasend)); size_t recvsize = sock.receive(boost::asio::buffer(datarecv)); std::cout << datarecv << std::endl; } catch (boost::system::system_error e) { std::cout << e.code()<< std::endl; } system("pause"); return 0; }
文章来源: boost asio 同步TCP客户端