xtd 0.2.0
Loading...
Searching...
No Matches

◆ bind()

template<typename end_point_t >
void xtd::net::sockets::socket::bind ( const end_point_t &  local_end_point)
inline

Associates a xtd::net::sockets::socket with a local endpoint.

Parameters
localEndPointThe local xtd::net::sockets::end_point to associate with the xtd::net::sockets::socket.
Exceptions
xtd::net::sockets::socket_exceptionAn error occurred when attempting to access the socket.
xtd::object_closed_exceptionThe xtd::net::sockets::socket has been closed.
Remarks
Use the xtd::net::sockets::socket::bind method if you need to use a specific local endpoint. You must call xtd::net::sockets::socket::bind before you can call the xtd::net::sockets::socket::socket::listen method. You do not need to call xtd::net::sockets::socket::bind before using the xtd::net::sockets::socket::connect method unless you need to use a specific local endpoint. You can use the xtd::net::sockets::socket::bind method on both connectionless and connection-oriented protocols.
Before calling xtd::net::sockets::socket::bind, you must first create the local xtd::net::ip_end_point from which you intend to communicate data. If you do not care which local address is assigned, you can create an xtd::net::ip_end_point using xtd::net::ip_address::any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an xtd::net::ip_end_point using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.
If you use the above approach, you can discover what local network address and port number has been assigned by calling the xtd::net::sockets::socket::local_dnd_point. If you are using a connection-oriented protocol, xtd::net::sockets::socket::local_end_point will not return the locally assigned network address until after you have made a call to the xtd::net::sockets::socket::connect or xtd::net::sockets::socket::end_connect method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.
If a UDP socket wants to receive interface information on received packets, the xtd::net::sockets::socket::set_socket_option method should be explicitly called with the socket option set to xtd::net::sockets::socket_option_name::packet_information immediately after calling the xtd::net::sockets::socket::bind method.
Note
If you intend to receive multicast datagrams, you must call the xtd::net::sockets::socket::bind method with a multicast port number.
You must call the xtd::net::sockets::socket::bind method if you intend to receive connectionless datagrams using the xtd::net::sockets::socket::receive_from method.
If you receive a xtd::net::sockets::socket_exception, use the xtd::net::sockets::socket_exception::error_code property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.
Examples
network_stream.cpp, socket_tcp_ip_v4.cpp, socket_tcp_ip_v4_without_thread.cpp, socket_tcp_ip_v6.cpp, socket_udp_ip_v4.cpp, and socket_udp_ip_v6.cpp.