一、同步处理socket
1、服务端
int main()
{
try
{
std::cout << "Serveer start." << std::endl;
io_service ios;
ip::tcp::acceptor acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688)); //创建acceptor对象,接受6688端口
std::cout << acceptor.local_endpoint().address << std::endl;
while (true) //服务一直执行
{
ip::tcp::socket sock(ios); //创建一个socket对象,
acceptor.accept(sock); //阻塞等待socket对象连接
std::cout << "client:";
std::cout << sock.remote_endpoint().address() << std::endl;
sock.write_some(buffer("hello asio.")); //发送数据 必须要用buffer包装
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
2、客户端