Skip to content

Commit 14f1b04

Browse files
authored
Merge pull request #30 from index1207/readme
Readme
2 parents 81f2149 + 163d6e1 commit 14f1b04

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

README.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# netcpp ![windows](https://github.com/index1207/netcpp/actions/workflows/windows.yml/badge.svg) ![linux](https://github.com/index1207/netcpp/actions/workflows/linux.yml/badge.svg) [![codecov](https://codecov.io/gh/index1207/netcpp/graph/badge.svg?_token=BVVUC5S422)](https://codecov.io/gh/index1207/netcpp) ![lang](https://img.shields.io/badge/language-C++20-blue) [![Vcpkg package](https://img.shields.io/badge/vcpkg-netcpp-blue)](https://github.com/microsoft/vcpkg/tree/master/ports/netcpp) [![License](https://img.shields.io/github/license/index1207/netcpp.svg)](LICENSE)
2-
netcpp is open-source simple C++ network library. netcpp supports windows and linux(ubuntu) platform. asynchronous feature implement by each os's api. Windows implemented using IOCP and Ubuntu will implement using Epoll.
1+
# 📦netcpp ![windows](https://github.com/index1207/netcpp/actions/workflows/windows.yml/badge.svg) ![linux](https://github.com/index1207/netcpp/actions/workflows/linux.yml/badge.svg) [![codecov](https://codecov.io/gh/index1207/netcpp/graph/badge.svg?_token=BVVUC5S422)](https://codecov.io/gh/index1207/netcpp) ![lang](https://img.shields.io/badge/language-C++20-blue) [![Vcpkg package](https://img.shields.io/badge/vcpkg-netcpp-blue)](https://github.com/microsoft/vcpkg/tree/master/ports/netcpp) [![License](https://img.shields.io/github/license/index1207/netcpp.svg)](LICENSE)
2+
netcpp is open-source simple C++ network library. netcpp supports windows and linux platform. asynchronous feature implement by each os's api. Windows implemented using IOCP and Linux implemented using io_uring.
33

44
## Installation
55
To use netcpp, create new application by vcpkg or enable manifest mode at Visual Studio. <br>
@@ -22,7 +22,35 @@ cmake -B build
2222
cmake --build build
2323
```
2424

25-
## Example and Features
25+
## Example
26+
- Create a socket
27+
```cpp
28+
// <net/socket.hpp>
29+
net::socket tcp_socket(net::protocol::tcp); // Create a TCP socket
30+
31+
net::socket udp_socket(net::protocol::udp); // Create a UDP socket
32+
33+
net::socket empty_socket;
34+
empty_socket.create(net::protocol::tcp); // Create a new tcp socket
35+
```
36+
- Async I/O
37+
```cpp
38+
// <net/context.hpp>
39+
net::socket sock(net::protocol::tcp); // Create a new TCP socket.
40+
net::context connect_ctx;
41+
connect_ctx.endpoint = ENDPOINT; // Specify the endpoint.
42+
connect_ctx.completed = [](net::context* ctx, bool success) { // callback
43+
if (success)
44+
{
45+
std::cout << "Connected" << std::endl;
46+
}
47+
else
48+
{
49+
std::cout << "Failed to connect" << std::endl;
50+
}
51+
};
52+
sock.connect(&connect_ctx); // Connect to specified endpoint asynchronously.
53+
```
2654
- Basic connection
2755
```cpp
2856
// Server
@@ -31,12 +59,12 @@ cmake --build build
3159

3260
int main()
3361
{
34-
net::Native::initialize(); // Initialize Native API
62+
net::native::initialize(); // Initialize Native API
3563

36-
net::Socket sock(net::protocol::tcp); // Create new TCP socket
37-
if (!sock.is_open()) // Invalidate socket
64+
net::socket sock(net::protocol::tcp); // Create new TCP socket
65+
if (!sock.is_open()) // Validate socket
3866
return -1;
39-
if(!sock.bind(net::Endpoint(net::IpAddress::loopback, 8085))) // Bind address
67+
if(!sock.bind(net::endpoint(net::ip_address::loopback, 8085))) // Bind the address `tcp://loopback:8085`
4068
return -1;
4169
if(!sock.listen()) // Ready to accept
4270
return -1;
@@ -50,21 +78,32 @@ int main()
5078
```
5179
```cpp
5280
// Client
53-
#include <net/Socket.hpp>
81+
#include <net/socket.hpp>
5482
#include <iostream>
5583

5684
int main()
5785
{
58-
net::Native::initialize(); // Initialize Native API
86+
net::native::initialize(); // Initialize Native API
5987

60-
net::Socket sock(net::protocol::tcp); // Create new TCP socket
88+
net::socket sock(net::protocol::tcp); // Create new TCP socket
6189
if (!sock.is_open()) // Invalidate socket
6290
return -1;
63-
if (!sock.connect(net::Endpoint(net::IpAddress::loopback, 8085))) // Try to connect to server.
91+
if (!sock.connect(net::endpoint(net::ip_address::loopback, 8085))) // Try to connect to server.
6492
return -1;
6593
std::cout << "Connected!";
6694
}
6795
```
96+
- DNS
97+
```cpp
98+
// <net/dns.hpp>
99+
net::dns::get_host_name() // get host's name
100+
101+
net::dns::get_host_entry("www.example.com") // get www.example.com's host entry
102+
```
103+
104+
## Contribute
105+
The repository is whenever welcome any issues or PRs!
106+
68107
## Minimum required compiler version
69108
- Windows
70109
- Visual Studio 2019

0 commit comments

Comments
 (0)