xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Go to the documentation of this file.
1 #pragma once
6 #include "tcp_client.h"
9 namespace xtd {
11  namespace net {
13  namespace sockets {
30  class tcp_listener : public xtd::object {
31  class async_result_socket : public xtd::object, public xtd::iasync_result {
32  public:
33  async_result_socket(std::any async_state) : async_state_(async_state) {}
34  std::any async_state() const noexcept override {return async_state_;}
35  std::shared_mutex& async_mutex() override {return async_mutex_;}
36  bool completed_synchronously() const noexcept override {return false;}
37  bool is_completed() const noexcept override {return is_completed_;};
39  std::any async_state_;
40  bool is_completed_ = false;
41  std::shared_mutex async_mutex_;
43  std::exception_ptr exception_;
44  };
46  class async_result_accept_socket : public async_result_socket {
47  public:
48  async_result_accept_socket(std::any async_state) : async_result_socket(async_state) {}
50  };
52  class async_result_accept_tcp_client : public async_result_socket {
53  public:
54  async_result_accept_tcp_client(std::any async_state) : async_result_socket(async_state) {}
56  };
58  public:
76  tcp_listener(const xtd::net::ip_address& ip_address, uint16_t port);
79  tcp_listener(tcp_listener&&) = default;
80  tcp_listener(const tcp_listener&) = default;
81  ~tcp_listener();
82  tcp_listener& operator=(const tcp_listener&) = default;
83  friend std::ostream& operator <<(std::ostream& os, const tcp_listener& tl) noexcept {return os << tl.to_string();}
84  bool operator==(const tcp_listener& s) const {return data_ == s.data_;};
85  bool operator!=(const tcp_listener& s) const {return !operator==(s);};
98  bool exclusive_address_use() const;
110  tcp_listener& exclusive_address_use(bool value);
115  const xtd::net::end_point& local_end_point() const noexcept;
122  xtd::net::sockets::socket server() const noexcept;
154  std::shared_ptr<xtd::iasync_result> begin_accept_socket(xtd::async_callback callback, const std::any& state);
165  std::shared_ptr<xtd::iasync_result> begin_accept_tcp_client(xtd::async_callback callback, const std::any& state);
170  static tcp_listener create(uint16_t port);
181  xtd::net::sockets::socket end_accept_socket(std::shared_ptr<xtd::iasync_result> async_result);
193  xtd::net::sockets::tcp_client end_accept_tcp_client(std::shared_ptr<xtd::iasync_result> async_result);
201  bool pending();
209  void start();
220  void start(size_t backlog);
227  void stop();
229  protected:
234  bool active() const noexcept;
236  private:
237  struct data;
238  std::shared_ptr<data> data_;
239  };
240  }
241  }
242 }
xtd::net::sockets::tcp_client accept_tcp_client()
Accepts a pending connection request.
Implements the Berkeley sockets interface.
Definition: socket.h:63
virtual xtd::ustring to_string() const noexcept
Returns a std::string that represents the current object.
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:30
Represents the status of an asynchronous operation.
Definition: iasync_result.h:21
Identifies a network address. This is an abstract class.
Definition: end_point.h:24
xtd::net::sockets::socket accept_socket()
Accepts a pending connection request.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
xtd::net::sockets::socket end_accept_socket(std::shared_ptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new Socket to handle remote host ...
bool exclusive_address_use() const
Gets a bool value that specifies whether the xtd::net::sockets::tcp_listener allows only one underlyi...
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:20
delegate< void(std::shared_ptr< xtd::iasync_result > ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition: async_callback.h:18
const xtd::net::end_point & local_end_point() const noexcept
Gets the underlying xtd::net::end_point of the current xtd::net::sockets::tcp_listener.
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:24
static tcp_listener create(uint16_t port)
Creates a new xtd::net::sockets::tcp_listener instance to listen on the specified port...
bool pending()
Determines if there are pending connection requests.
Provides client connections for TCP network services.
Definition: tcp_client.h:39
std::shared_ptr< xtd::iasync_result > begin_accept_tcp_client(xtd::async_callback callback, const std::any &state)
Begins an asynchronous operation to accept an incoming connection attempt.
std::shared_ptr< xtd::iasync_result > begin_accept_socket(xtd::async_callback callback, const std::any &state)
Begins an asynchronous operation to accept an incoming connection attempt.
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
void stop()
Closes the listener.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:26
Contains xtd::net::sockets::tcp_client class.
void start()
xtd::net::sockets::tcp_listener::starts listening for incoming connection requests.
tcp_listener(const xtd::net::ip_end_point &local_end_point)
Initializes a new instance of the xtd::net::sockets::tcp_listener class with the specified local endp...
bool active() const noexcept
Gets a value that indicates whether xtd::net::sockets::tcp_listener is actively listening for client ...
xtd::net::sockets::tcp_client end_accept_tcp_client(std::shared_ptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new xtd::net::sockets::tcp_client...
Listens for connections from TCP network clients.
Definition: tcp_listener.h:30
xtd::net::sockets::socket server() const noexcept
Gets the underlying network xtd::net::sockets::socket.