accepting a socket

This commit is contained in:
Антон 2024-03-30 07:54:20 +03:00
parent 810575f37e
commit 162165a7a2
2 changed files with 36 additions and 0 deletions

View File

@ -53,3 +53,8 @@ add_executable(connecting_a_socket
connecting_a_socket.cpp
src/sdk.h)
target_link_libraries(connecting_a_socket PRIVATE Threads::Threads)
add_executable(accepting_connections.cpp
accepting_connections.cpp
src/sdk.h)
target_link_libraries(accepting_connections.cpp PRIVATE Threads::Threads)

View File

@ -0,0 +1,31 @@
#include "src/sdk.h"
#include <boost/asio.hpp>
#include <iostream>
using namespace boost;
namespace sys = boost::system;
namespace net = boost::asio;
using namespace std;
int main()
{
constexpr int32_t k_BacklogSize = 30;
constexpr uint16_t k_Port = 3333;
net::ip::tcp::endpoint ep(net::ip::address_v4::any(), k_Port);
net::io_context io_context;
try
{
net::ip::tcp::acceptor acceptor(io_context, ep.protocol());
acceptor.bind(ep);
acceptor.listen(k_BacklogSize);
net::ip::tcp::socket socket(io_context);
acceptor.accept(socket);
}
catch (sys::system_error& e)
{
cout << "Error: "s << e.code() << endl;
}
return EXIT_SUCCESS;
}