Socket

There are 4 types of socket:

(1) basic_stream_socket:
This socket provides sequenced, reliable, two-way connection based byte streams. tcp::socket is an instance of this socket:

  1. class tcp
  2. {
  3. ......
  4. /// The TCP socket type.
  5. typedef basic_stream_socket<tcp> socket;
  6. ......
  7. }

(2) basic_datagram_socket:
This socket provides connectionless, datagram service. udp::socket is an instance of this socket:

  1. class udp
  2. {
  3. ......
  4. /// The UDP socket type.
  5. typedef basic_datagram_socket<udp> socket;
  6. ......
  7. }

(3) basic_raw_socket:
This socket provides access to internal network protocols and interfaces. icmp::socket is an instance of this socket:

  1. class icmp
  2. {
  3. ......
  4. /// The ICMP socket type.
  5. typedef basic_raw_socket<icmp> socket;
  6. ......
  7. }

(4) basic_seq_packet_socket:
This socket combines stream and datagram: it provides a sequenced, reliable, two-way connection based datagrams service. SCTP is an example of this type of service.

All these 4 sockets derive from basic_socket class, and need to associate with an io_context during initialization. Take tcp::socket as an example:

  1. boost::asio::io_context io_context;
  2. boost::asio::ip::tcp::socket socket{io_context};

Please notice the io_context should be a reference in constructor of socket (Please refer io_context). Still use basic_socket an an instance, one of its constructor is like following:

  1. explicit basic_socket(boost::asio::io_context& io_context)
  2. : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
  3. {
  4. }

For basic_io_object class, it does not support copy constructed/copy assignment:

  1. ......
  2. private:
  3. basic_io_object(const basic_io_object&);
  4. void operator=(const basic_io_object&);
  5. ......

whilst it can be movable:

  1. ......
  2. protectd:
  3. basic_io_object(basic_io_object&& other)
  4. {
  5. ......
  6. }
  7. basic_io_object& operator=(basic_io_object&& other)
  8. {
  9. ......
  10. }