1- # TCP C++ client/server with SSL/TLS support (header file only)
1+ # C++ client/server with SSL/TLS support (header file only)
22[ ![ MIT license] ( https://img.shields.io/badge/license-MIT-blue.svg )] ( http://opensource.org/licenses/MIT )
3- ![ Cmake Build] ( https://github.com/martelkr/cppsocket/actions/workflows/cmake.yml/badge.svg )
3+ ![ cmake Build] ( https://github.com/martelkr/cppsocket/actions/workflows/cmake.yml/badge.svg )
4+ ![ clang Build] ( https://github.com/martelkr/cppsocket/actions/workflows/clang.yml/badge.svg )
5+ [ ![ Coverage Status] ( https://coveralls.io/repos/github/martelkr/cppsocket/badge.svg )] ( https://coveralls.io/github/martelkr/cppsocket )
46
57## About
6- This is a header file only implementation of a C++ TCP client/server with or without SSL/TLS for Linux only .
8+ This is a header file only implementation of a C++ client/server with or without SSL/TLS/DTLS .
79The implementation uses OpenSSL and BSD API to implement the underlying socket interfaces.
810
911Compilation has been tested with:
1012- GCC 11.3.0 (GNU/Linux Ubuntu 22.04.1 LTS)
13+ - cmake 3.22.1
14+ - googletest 1.11.0-3
15+ - Visual Studio Community 2022 17.4.4 (64-bit) (Windows 11)
16+ - cmake 3.26.0-rc1
17+ - googletest 1.13.0
1118
1219## Usage
20+
21+ ### TCP server/client
22+
1323Create a TCP server object for accepting TCP connections.
1424
1525``` cpp
@@ -20,7 +30,7 @@ TCPServer(void);
2030TCPServer(const std::string& keyFile, const std::string& certFile);
2131
2232// No SSL and IP/port bound
23- explicit TCPServer(const uint16_t port, const std::string& ip = "", const int backlog = 3);
33+ explicit TCPServer(const uint16_t port, const std::string& ip = "0.0.0.0 ", const int backlog = 3);
2434
2535/// SSL and IP/port bound
2636TCPServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile, const int backlog = 3);
@@ -39,7 +49,7 @@ For a BSD-like approach, the following sequence can be followed:
3949// Server
4050
4151// create server socket
42- TCPServer s;
52+ TCPServer s; // add key file and cert file here for secure connection
4353
4454// bind to port 54321 on IP 0.0.0.0
4555s.bindAndListen(54321 );
@@ -51,20 +61,82 @@ TCPClient c = s.accept();
5161// Client
5262
5363// Connect to TCP server on IP 127.0.0.1 and port 54321
54- TCPClient c ("127.0.0.1", 54321);
64+ TCPClient c ("127.0.0.1", 54321); // add key file and cert file here for secure connection
65+ ```
66+
67+ ### UDP server/client
68+
69+ Create a UDP server object for accepting UDP connections.
70+
71+ ```cpp
72+ // default constructor creates unbound unsecure UDP server socket
73+ UDPServer(void);
74+
75+ // default DTLS constructor create unbound UDP server socket ready for DTLS
76+ // NOTE: UDPServer s("", ""); results in unbound unsecure UDP server socket
77+ UDPServer(const std::string& keyFile, const std::string& certFile);
78+
79+ // creates unsecure UDP server socket bound to specific port and IP address (default all host IP)
80+ explicit UDPServer(const uint16_t port, const std::string& ip = "0.0.0.0");
81+
82+ // creates bound UDP server socket ready for DTLS
83+ // NOTE: UDPServer s("", ""); results in unbound unsecure UDP server socket
84+ UDPServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile);
85+ ```
86+
87+ Create a UDP client object to connect to a known UDP server.
88+
89+ ``` cpp
90+
91+ // default constructor creates unconnected UDP client socket
92+ UDPClient (void);
93+
94+ // creates UDP client socket connected to UDP server
95+ UDPClient(const std::string& remoteIp, const uint16_t remotePort);
96+
97+ // creates unconnected UDP client socket for DTLS communication
98+ UDPClient(const std::string& keyFile, const std::string& certFile);
99+
100+ // created UDP client socket connected to UDP server using DTLS
101+ UDPClient(const std::string& remoteIp, const uint16_t remotePort, const std::string& keyFile, const std::string& certFile);
102+ ```
103+
104+ For a BSD-like approach, the following sequence can be followed:
105+
106+ ```cpp
107+ // Server
108+
109+ // create server socket
110+ UDPServer s; // add key file and cert file here for secure connection
111+
112+ // bind to port 54321 on IP 0.0.0.0
113+ s.bind(54321);
114+
115+ // following not needed for unsecure connection but is needed for DTLS connection
116+ s.accept();
117+ ```
118+
119+ ``` cpp
120+ // Client
121+
122+ // Connect to UDP server on IP 127.0.0.1 and port 54321
123+ UDPClient c ("127.0.0.1", 54321); // add key file and cert file here for secure connection
55124```
56125
57126## Thread Safety
58127
59- Do not share TCPClient objects across threads unless you provide your own thread safety on the send/read calls.
128+ Do not share TCPClient, UDPClient or UDPServer objects across threads unless you provide your own thread safety on the send/read calls.
60129
61130## Installation
62131
63132Use the `cppsocket.hpp` file in your source tree and include it in the file that need to use it.
64133
65134## Run Unit Tests
66135
67- There are two basic unit tests included in `test/TestCppSocket.cpp` to test basic data passing for secure and unsecure TCP sockets.
136+ Unit tests run with ctest:
137+ ```
138+ ctest -C debug
139+ ```
68140
69141## CppCheck Compliancy
70142
0 commit comments