writing to socket
This commit is contained in:
parent
faf9cf0c3e
commit
cbbf4ab7e2
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue