Boost C++ Libraries/Boost.Asio

Boost.Asio 튜토리얼 - Daytime.5 - 동기식 UDP daytime 서버

까마귀75 2020. 12. 17. 10:08
728x90
반응형

Daytime.5 - 동기식 UDP daytime 서버 (A synchronous UDP daytime server)

이 튜토리얼 프로그램은 asio를 사용하여 UDP용 서버 응용프로그램을 구현하는 방법을 보여준다.

int main()
{
  try
  {
    boost::asio::io_context io_context;


UDP 포트 13번에서 요청을 수신 할 ip::udp::socket 개체를 생성한다.

    udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));


클라이언트에서 요청을 시작할 때까지 기다린다. remote_endpoint 개체는 ip::udp::socket::receive_from()에 파라미터로 주어지게 된다.

    for (;;)
    {
      boost::array<char, 1> recv_buf;
      udp::endpoint remote_endpoint;
      socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint);


클라이언트에 응답할 데이터를 설정한다.

      std::string message = make_daytime_string();


remote_endpoint에 응답을 보낸다.

      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message),
          remote_endpoint, 0, ignored_error);
    }
  }


마지막으로 모든 예외를 처리한다.

  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}


전체 소스 보기

돌아가기: 튜토리얼 페이지

이전: Daytime.4 - 동기식 UDP daytime 클라이언트
다음: Daytime.6 - 비동기식 UDP daytime 서버

원본 링크

728x90
반응형