boost_asio/boost_boilerplate/src/chapter02_io/writing_tcp_sync.cpp

41 lines
901 B
C++

#include <boost/asio.hpp>
#include <iostream>
#include <memory>
using namespace boost;
using namespace std;
void WriteToSocket(asio::ip::tcp::socket& sock)
{
string buf = "Hello"s;
size_t total_bytes_written {};
while (total_bytes_written != buf.length())
{
total_bytes_written += sock.write_some(asio::buffer(buf.c_str() + total_bytes_written,
buf.length() - total_bytes_written));
}
}
int main()
{
string raw_ip_address = "127.0.0.1";
size_t port_num = 3333;
try
{
asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num);
asio::io_service io_service;
asio::ip::tcp::socket sock(io_service, ep.protocol());
sock.connect(ep);
WriteToSocket(sock);
}
catch (const system::system_error& e)
{
cout << "Error! " << e.what() << endl;
}
return EXIT_SUCCESS;
}