xtd 0.2.0
Loading...
Searching...
No Matches
xtd::net::sockets::udp_client Class Reference
Inheritance diagram for xtd::net::sockets::udp_client:
xtd::object xtd::iequatable< udp_client > xtd::interface xtd::equality_operators< type_t, equatable_t >

Definition

Provides User Datagram Protocol (UDP) network services.

class coore_export_ udp_client : public xtd::object
Provides User Datagram Protocol (UDP) network services.
Definition udp_client.h:45
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Inheritance
xtd::objectxtd::net::sockets::udp_client
Header
#include <xtd/net/sockets/udp_client>
Namespace
xtd::net::sockets
Library
xtd.core
Remarks
The xtd::net::sockets::udp_client class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways:
You can use any of the send methods provided in the xtd::net::sockets::udp_client to send data to a remote device. Use the xtd::net::sockets::udp_client::receive method to receive data from remote hosts.
Note
Do not call xtd::net::sockets::udp_client::send using a host name or xtd::net::ip_end_point if you have already specified a default remote host. If you do, xtd::net::sockets::udp_client will throw an exception.
Remarks
xtd::net::sockets::udp_client methods also allow you to send and receive multicast datagrams. Use the xtd::net::sockets::udp_client::join_multicast_group method to subscribe a xtd::net::sockets::udp_client to a multicast group. Use the xtd::net::sockets::udp_client::drop_multicast_group method to unsubscribe a xtd::net::sockets::udp_client from a multicast group.
Examples
The following example shows how to use IPv4 xtd::net::sockets::udp_client class.
#include <xtd/collections/generic/list>
#include <xtd/io/stream_reader>
#include <xtd/net/sockets/udp_client>
#include <xtd/net/ip_end_point>
#include <xtd/threading/thread>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main() -> int {
auto terminate_app = false;
auto server = thread {[&] {
while (!terminate_app) {
ip_end_point incoming_end_point;
auto buffer = udp.receive(incoming_end_point);
if (buffer.size() && buffer[0] != 0xFF)
console::write_line(string(buffer.begin(), buffer.end()));
}
}};
auto client = thread {[&] {
auto udp = udp_client {};
auto counter = 0;
while (!terminate_app) {
auto str = string::format("counter={}", ++counter);
udp.send(list<unsigned char>(str.begin(), str.end()), str.size(), ip_end_point(ip_address::loopback, 9400));
thread::sleep(50_ms);
}
}};
server.start();
client.start();
terminate_app = true;
server.join();
client.join();
}
// This code produces the following output :
//
// counter=1
// counter=2
// counter=3
// counter=4
// counter=5
// ...
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::collections::generic::list::...
Definition list.h:368
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static const ip_address any
Provides an IP address that indicates that the server must listen for client activity on all network ...
Definition ip_address.h:44
static const ip_address loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.h:60
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.h:23
xtd::net::sockets::socket client() const noexcept
Gets the underlying network xtd::net::sockets::socket.
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:43
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
@ udp
User Datagram Protocol.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition dictionary.h:15
The xtd::net::sockets namespace provides a managed implementation of the Berkeley Sockets interface f...
Definition address_family.h:16
The xtd::net namespace provides a simple programming interface for many of the protocols used on netw...
Definition cookie_exception.h:10
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
The following example shows how to use IPv6 xtd::net::sockets::udp_client class.
#include <xtd/collections/generic/list>
#include <xtd/io/stream_reader>
#include <xtd/net/sockets/udp_client>
#include <xtd/net/ip_end_point>
#include <xtd/threading/thread>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::net;
using namespace xtd::net::sockets;
using namespace xtd::threading;
auto main() -> int {
auto terminate_app = false;
auto server = thread {[&] {
while (!terminate_app) {
auto incoming_end_point = ip_end_point {};
auto buffer = udp.receive(incoming_end_point);
if (buffer.size() && buffer[0] != 0xFF)
console::write_line(string(buffer.begin(), buffer.end()));
}
}};
auto client = thread {[&] {
auto counter = 0;
while (!terminate_app) {
auto str = string::format("counter={}", ++counter);
udp.send(list<unsigned char>(str.begin(), str.end()), str.size(), ip_end_point(ip_address::ip_v6_loopback, 9400));
thread::sleep(50_ms);
}
}};
server.start();
client.start();
terminate_app = true;
server.join();
client.join();
}
// This code produces the following output :
//
// counter=1
// counter=2
// counter=3
// counter=4
// counter=5
// ...
static const ip_address ip_v6_loopback
Provides the IP loopback address. This field is constant.
Definition ip_address.h:53
static const ip_address ip_v6_any
The Socket::Bind method uses the cIPv6Any field to indicate that a Socket must listen for client acti...
Definition ip_address.h:50
@ inter_network_v6
Address for IP version 6.
Examples
udp_client_ip_v4.cpp, and udp_client_ip_v6.cpp.

Public Constructors

 udp_client ()
 Initializes a new instance of the xtd::net::sockets::udp_client class.
 
 udp_client (uint16 port)
 Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the local port number provided.
 
 udp_client (const xtd::net::ip_end_point &local_end_point)
 Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the specified local endpoint.
 
 udp_client (xtd::net::sockets::address_family address_Family)
 Initializes a new instance of the xtd::net::sockets::udp_client class.
 
 udp_client (uint16 port, xtd::net::sockets::address_family addressFamily)
 Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the local port number provided.
 
 udp_client (const xtd::string &hostname, uint16 port)
 Initializes a new instance of the xtd::net::sockets::udp_client class and establishes a default remote host.
 

Public Properties

size_t available () const
 Gets the amount of data received from the network that is available to read.
 
xtd::net::sockets::socket client () const noexcept
 Gets the underlying network xtd::net::sockets::socket.
 
udp_clientclient (const xtd::net::sockets::socket &value) noexcept
 Sets the underlying network xtd::net::sockets::socket.
 
bool dont_fragment () const
 Gets a boolean value that specifies whether the xtd::net::sockets::udp_client allows Internet Protocol (IP) datagrams to be fragmented.
 
udp_clientdont_fragment (bool value)
 Sets boolean value that specifies whether the xtd::net::sockets::udp_client allows Internet Protocol (IP) datagrams to be fragmented.
 
bool enable_broadcast () const
 Gets a boolean value that specifies whether the xtd::net::sockets::udp_client may send or receive broadcast packets.
 
udp_clientenable_broadcast (bool value)
 Sets a boolean value that specifies whether the xtd::net::sockets::udp_client may send or receive broadcast packets.
 
bool exclusive_address_use () const
 Gets a boolean value that specifies whether the xtd::net::sockets::udp_client allows only one client to use a port.
 
udp_clientexclusive_address_use (bool value)
 Sets a boolean value that specifies whether the xtd::net::sockets::udp_client allows only one client to use a port.
 
bool multicast_loopback () const
 Gets a boolean value that specifies whether outgoing multicast packets are delivered to the sending application.
 
udp_clientmulticast_loopback (bool value)
 Sets a boolean value that specifies whether outgoing multicast packets are delivered to the sending application.
 
xtd::byte ttl () const
 Gets a value that specifies the Time to Live (TTL) value of Internet Protocol (IP) packets sent by the xtd::net::sockets::udp_client.
 
udp_clientttl (xtd::byte value)
 Sets a value that specifies the Time to Live (TTL) value of Internet Protocol (IP) packets sent by the xtd::net::sockets::udp_client.
 

Public Methods

void allow_nat_traversal (bool allowed)
 Enables or disables Network Address Translation (NAT) traversal on a xtd::net::sockets::udp_client instance.
 
xtd::sptr< xtd::iasync_resultbegin_receive (xtd::async_callback callback, const std::any &state)
 xtd::net::sockets::udp_client::receives a datagram from a remote host asynchronously.
 
xtd::sptr< xtd::iasync_resultbegin_send (const std::vector< xtd::byte > &dgram, size_t bytes, const xtd::string &hostname, uint16 port, xtd::async_callback callback, const std::any &state)
 xtd::net::sockets::udp_client::sends a datagram to a destination asynchronously. The destination is specified by the host name and port number.
 
xtd::sptr< xtd::iasync_resultbegin_send (const std::vector< xtd::byte > &dgram, size_t bytes, const xtd::net::ip_end_point &end_point, xtd::async_callback callback, const std::any &state)
 xtd::net::sockets::udp_client::sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.
 
xtd::sptr< xtd::iasync_resultbegin_send (const std::vector< xtd::byte > &dgram, size_t bytes, xtd::async_callback callback, const std::any &state)
 xtd::net::sockets::udp_client::sends a datagram to a remote host asynchronously. The destination was specified previously by a call to xtd::net::sockets::udp_client::connect.
 
void close ()
 Closes the UDP connection.
 
void connect (const xtd::net::ip_end_point &end_point)
 Establishes a default remote host using the specified network endpoint.
 
void connect (const xtd::net::ip_address &ip_address, uint16 port)
 Establishes a default remote host using the specified IP address and port number.
 
void connect (const xtd::string &hostname, uint16 port)
 Establishes a default remote host using the specified hostname and port number.
 
void drop_multicast_group (const xtd::net::ip_address &multicast_address)
 Leaves a multicast group.
 
void drop_multicast_group (const xtd::net::ip_address &multicast_address, uint32 if_index)
 Leaves a multicast group.
 
std::vector< xtd::byteend_receive (xtd::sptr< xtd::iasync_result > async_result, xtd::net::ip_end_point &remote_end_point)
 Ends a pending asynchronous receive.
 
size_t end_send (xtd::sptr< xtd::iasync_result > async_result)
 Ends a pending asynchronous send.
 
bool equals (const udp_client &s) const noexcept override
 
void join_multicast_group (const xtd::net::ip_address &multicast_address)
 Adds a xtd::net::sockets::udp_client to a multicast group.
 
void join_multicast_group (uint32 if_index, const xtd::net::ip_address &multicast_address)
 Adds a xtd::net::sockets::udp_client to a multicast group.
 
void join_multicast_group (const xtd::net::ip_address &multicast_address, xtd::byte ttl)
 Adds a xtd::net::sockets::udp_client to a multicast group with the specified Time to Live (TTL).
 
void join_multicast_group (const xtd::net::ip_address &multicast_address, const xtd::net::ip_address &local_address)
 Adds a xtd::net::sockets::udp_client to a multicast group.
 
std::vector< xtd::bytereceive (xtd::net::ip_end_point &remote_end_point)
 Returns a UDP datagram that was sent by a remote host.
 
size_t send (const std::vector< xtd::byte > &dgram, size_t bytes, const xtd::string &hostname, uint16 port)
 xtd::net::sockets::udp_client::sends a UDP datagram to a specified port on a specified remote host.
 
size_t send (const std::vector< xtd::byte > &dgram, size_t bytes, const xtd::net::ip_end_point &end_point)
 xtd::net::sockets::udp_client::sends a UDP datagram to the host at the specified remote endpoint.
 
size_t send (const std::vector< xtd::byte > &dgram, size_t bytes)
 xtd::net::sockets::udp_client::sends a UDP datagram to a remote host.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 

Protected Properties

bool active () const noexcept
 Gets a value that indicates whether a connection has been made.
 
udp_clientactive (bool value) noexcept
 Sets a value that indicates whether a connection has been made.
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Public Member Functions inherited from xtd::iequatable< udp_client >
virtual bool equals (const udp_client &) const noexcept=0
 Indicates wheth er the current object is equal to another object of the same type.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

The documentation for this class was generated from the following file: