netcpp
0.5.0
[한국어]
netcpp adalah pustaka jaringan C++ sederhana . Ini mendukung platform windows dan linux.
Perpustakaan ini mendukung port vcpkg. Jika Anda sudah menginstal vcpkg, Anda dapat menginstal paket ini hanya dengan baris perintah di bawah ini.
vcpkg install netcpp # In classic mode
vcpkg add port netcpp # In manifest mode
Atau kloning repositori ini, dan jalankan baris perintah di bawah ini.
git clone https://github.com/index1207/netcpp.git && cd netcpp # clone and move directory
cmake -B build # CMake Configuration
cmake --build build --config < BUILD_MODE > # Build library
cmake --install build --config < BUILD_MODE > --prefix < PATH_TO_INSTALL > # Install to other project
netcpp menyediakan target CMake:
find_package (netcpp CONFIG REQUIRED)
target_link_libraries (main PRIVATE netcpp::netcpp)
Pilihan | Keterangan |
---|---|
NETCPP_BUILD_SHARED | Bangun dengan perpustakaan bersama |
NETCPP_TEST | Sertakan pengujian unit dalam build |
// <net/socket.hpp>
net::socket tcp_socket (net::protocol::tcp); // Create a TCP socket
net::socket udp_socket (net::protocol::udp); // Create a UDP socket
net::socket empty_socket;
empty_socket.create(net::protocol::tcp); // Create a new TCP socket
// <net/context.hpp>
net::socket sock (net::protocol::tcp); // Create a new TCP socket.
net::context connect_ctx; // A context that necessary to async I/O
connect_ctx.endpoint = ENDPOINT; // Specify the endpoint.
connect_ctx.completed = [](net::context* ctx, bool success) { // callback
if (success)
{
std::cout << " Connected " << std::endl;
}
else
{
std::cout << " Failed to connect " << std::endl;
}
};
sock.connect(&connect_ctx); // Connect to specified endpoint asynchronously.
// Server
# include < net/Socket.hpp >
# include < iostream >
int main ()
{
net::native::initialize (); // Initialize Native API
net::socket sock (net::protocol::tcp); // Create new TCP socket
if (!sock. is_open ()) // Validate socket
return - 1 ;
if (!sock. bind ( net::endpoint (net::ip_address::loopback, 8085 ))) // Bind the address `tcp://loopback:8085`
return - 1 ;
if (!sock. listen ()) // Ready to accept
return - 1 ;
while ( true )
{
auto client = sock. accept (); // accept other client. it returns new client socket.
std::cout << " Connected n " ;
}
}
// Client
# include < net/socket.hpp >
# include < iostream >
int main ()
{
net::native::initialize (); // Initialize Native API
net::socket sock (net::protocol::tcp); // Create new TCP socket
if (!sock. is_open ()) // Validate socket
return - 1 ;
if (!sock. connect ( net::endpoint (net::ip_address::loopback, 8085 ))) // Try to connect to server.
return - 1 ;
std::cout << " Connected! " ;
}
// <net/dns.hpp>
net::dns::get_host_name () // get host's name
net::dns::get_host_entry( " www.example.com " ) // get www.example.com's host entry
try {
if (!sock. connect (ENDPOINT))
throw net::network_exception ( " connect() " );
}
catch (std:: exception & e) {
std::cout << e. what () << std::endl; // connect(): Cannot assign requested address. [10049]
}
sistem operasi | Perpustakaan |
---|---|
jendela | Winsock2 |
Linux | libur |
Repositori ini selalu menerima masalah atau PR apa pun!