Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
martelkr committed Dec 18, 2023
1 parent 351831d commit d06b2b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 40 deletions.
55 changes: 16 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,84 +97,61 @@ For a BSD-like approach, the following sequence can be followed:
```cpp
// Server
// create server socket
TcpServer server; // add key file and cert file here for secure connection
// bind to port 54321 on IP 0.0.0.0
server.bindAndListen(54321);
// create server socket and bind to all IP and 54321 port
TcpServer server(54321);
// wait for a client connection
TcpClient client = server.accept();
```

```cpp
// Client

// Connect to TCP server on IP 127.0.0.1 and port 54321
TcpClient client("127.0.0.1", 54321); // add key file and cert file here for secure connection
TcpClient client("127.0.0.1", 54321);
```
### UDP server/client
Create a UDP server object for accepting UDP connections.
```cpp
// default constructor creates unbound unsecure UDP server socket
UdpServer();
// default DTLS constructor create unbound UDP server socket ready for DTLS
// NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
UdpServer(const std::string& keyFile, const std::string& certFile);
// default no IP/port bound but UDP socket created
UdpServer();
// creates unsecure UDP server socket bound to specific port and IP address (default all host IP)
explicit UdpServer(const uint16_t port, const std::string& ip = "0.0.0.0");
// creates bound UDP server socket ready for DTLS
// NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
UdpServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile);
// UDP server socket created, IP/port bound
UdpServer(const uint16_t port, const std::string &ipAddr = "0.0.0.0");
```

Create a UDP client object to connect to a known UDP server.

```cpp
// default UDP socket created but no server connection
UdpClient();

// default constructor creates unconnected UDP client socket
UDPClient();

// creates UDP client socket connected to UDP server
UDPClient(const std::string& remoteIp, const uint16_t remotePort);

// creates unconnected UDP client socket for DTLS communication
UDPClient(const std::string& keyFile, const std::string& certFile);

// created UDP client socket connected to UDP server using DTLS
UDPClient(const std::string& remoteIp, const uint16_t remotePort, const std::string& keyFile, const std::string& certFile);
// UdpClient connected to a UdpServer IP/port
UdpClient(const std::string& ipAddr, const uint16_t port) noexcept(false)
```
For a BSD-like approach, the following sequence can be followed:
```cpp
// Server
// create server socket
UdpServer server; // add key file and cert file here for secure connection
// bind to port 54321 on IP 0.0.0.0
server.bind(54321);
// following not needed for unsecure connection but is needed for DTLS connection
server.accept();
// create server socket bound to all IP and 54321 port
UdpServer server(54321);
```

```cpp
// Client

// Connect to UDP server on IP 127.0.0.1 and port 54321
UDPClient client("127.0.0.1", 54321); // add key file and cert file here for secure connection
UDPClient client("127.0.0.1", 54321);
```
## Thread Safety
Do not share TcpServer, TcpClient, UDPClient or UdpServer objects across threads unless you provide your own thread safety on the send/read and accept calls.
Do not share any of the socket objects across threads unless you provide your own thread safety on the send/read and accept calls.
## Installation
Expand Down
2 changes: 1 addition & 1 deletion inc/cppsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ namespace com::github::socket
UdpServer::init();
}

UdpServer(const std::string& ipAddr, const uint16_t port) noexcept(false)
explicit UdpServer(const uint16_t port, const std::string &ipAddr = "0.0.0.0") noexcept(false)
: UdpServer()
{
if (!bind(ipAddr, port))
Expand Down

0 comments on commit d06b2b0

Please sign in to comment.