writing to socket

This commit is contained in:
Антон 2024-04-01 17:17:52 +03:00
parent faf9cf0c3e
commit cbbf4ab7e2
2 changed files with 45 additions and 0 deletions

View File

@ -73,3 +73,8 @@ add_executable(stream_oriented_buffers
src/chapter02_io/stream_oriented_buffers.cpp
src/sdk.h)
target_link_libraries(stream_oriented_buffers PRIVATE Threads::Threads)
add_executable(writing_tcp_sync
src/chapter02_io/writing_tcp_sync.cpp
src/sdk.h)
target_link_libraries(writing_tcp_sync PRIVATE Threads::Threads)

View File

@ -0,0 +1,40 @@
#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;
}