@@ -38,118 +38,120 @@ Socket::initSocket(domain, type, protocol); // creates socket
3838// create socket from previously created file descriptor
3939Socket(int);
4040
41- // create socket
41+ // create socket of the given type
4242Socket(domain, type, protocol);
43+
44+
4345```
4446
4547### TCP server/client
4648
4749Create a TCP server object for accepting TCP connections.
4850
4951```cpp
50- // default no SSL and not IP/port bound
52+ // default no IP/port bound but TCP socket created
5153TcpServer();
5254
53- // default SSL and not IP/port bound
54- TcpServer(const std::string& keyFile, const std::string& certFile);
55-
56- // No SSL and IP/port bound
57- explicit TcpServer(const uint16_t port, const std::string& ip = "0.0.0.0", const int backlog = 3);
55+ // TCP socket created, IP/port bound, but not listening
56+ TcpServer(const uint16_t port, const std::string& ipAddr = "0.0.0.0");
5857
59- /// SSL and IP/port bound
60- TcpServer(const uint16_t port, const std::string& ip , const std::string& keyFile , const std::string& certFile, const int backlog = 3 );
58+ // TCP socket created, IP/port bound, and listening for clients
59+ TcpServer(const std::string& ipAddr , const uint16_t port , const int backlog);
6160```
6261
6362Create a TCP client object to connect to a known TCP server.
6463
6564``` cpp
66- TcpClient (const std::string& ip, const uint16_t port, const bool ssl = false);
67- explicit TcpClient(const int fd, SSL_CTX* sslctx = nullptr);
65+ // default TCP socket created but no server connection
66+ TcpClient ();
67+
68+ // create TcpClient with given TCP socket file descriptor
69+ TcpClient (const int filedescriptor);
70+
71+ // TcpClient connected to a TcpServer IP/port
72+ TcpClient(const std::string& ipAddr, const uint16_t port);
73+ ```
74+
75+ Create a SSL TCP Server for accepting SSL TCP clients.
76+
77+ ```cpp
78+ // Create a SSL TCP Server not bound to IP/port
79+ SecureTcpServer(const std::string& keyFile, const std::string& certFile);
80+
81+ // Create a SSL TCP Server bound to a given port and IP or default IP
82+ SecureTcpServer(const std::string& keyFile, const std::string& certFile, const uint16_t port, const std::string& ipAddr = "0.0.0.0");
83+ ```
84+
85+ Create a SSL TCP client for connecting to SSL TCP servers.
86+
87+ ``` cpp
88+ // create a SSL TCP client with a given SSL context - used with SecureTcpServer::accept return
89+ SecureTcpClient (const int filedescriptor, SSL_CTX * sslctx);
90+
91+ // create a SSL TCP client connected to a SSL TCP server
92+ SecureTcpClient(const std::string& ipAddr, const uint16_t port);
6893```
6994
7095For a BSD-like approach, the following sequence can be followed:
7196
7297```cpp
7398// Server
7499
75- // create server socket
76- TcpServer server; // add key file and cert file here for secure connection
77-
78- // bind to port 54321 on IP 0.0.0.0
79- server.bindAndListen(54321);
100+ // create server socket and bind to all IP and 54321 port
101+ TcpServer server(54321);
80102
103+ // wait for a client connection
81104TcpClient client = server.accept();
82105```
83106
84107``` cpp
85108// Client
86109
87110// Connect to TCP server on IP 127.0.0.1 and port 54321
88- TcpClient client ("127.0.0.1", 54321); // add key file and cert file here for secure connection
111+ TcpClient client ("127.0.0.1", 54321);
89112```
90113
91114### UDP server/client
92115
93116Create a UDP server object for accepting UDP connections.
94117
95118```cpp
96- // default constructor creates unbound unsecure UDP server socket
97- UdpServer();
98-
99- // default DTLS constructor create unbound UDP server socket ready for DTLS
100- // NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
101- UdpServer(const std::string& keyFile, const std::string& certFile);
102-
103- // creates unsecure UDP server socket bound to specific port and IP address (default all host IP)
104- explicit UdpServer(const uint16_t port, const std::string& ip = "0.0.0.0");
119+ // default no IP/port bound but UDP socket created
120+ UdpServer();
105121
106- // creates bound UDP server socket ready for DTLS
107- // NOTE: UdpServer s("", ""); results in unbound unsecure UDP server socket
108- UdpServer(const uint16_t port, const std::string& ip, const std::string& keyFile, const std::string& certFile);
122+ // UDP server socket created, IP/port bound
123+ UdpServer(const uint16_t port, const std::string &ipAddr = "0.0.0.0");
109124```
110125
111126Create a UDP client object to connect to a known UDP server.
112127
113128``` cpp
129+ // default UDP socket created but no server connection
130+ UdpClient ();
114131
115- // default constructor creates unconnected UDP client socket
116- UDPClient ();
117-
118- // creates UDP client socket connected to UDP server
119- UDPClient (const std::string& remoteIp, const uint16_t remotePort);
120-
121- // creates unconnected UDP client socket for DTLS communication
122- UDPClient(const std::string& keyFile, const std::string& certFile);
123-
124- // created UDP client socket connected to UDP server using DTLS
125- UDPClient(const std::string& remoteIp, const uint16_t remotePort, const std::string& keyFile, const std::string& certFile);
132+ // UdpClient connected to a UdpServer IP/port
133+ UdpClient (const std::string& ipAddr, const uint16_t port) noexcept(false)
126134```
127135
128136For a BSD-like approach, the following sequence can be followed:
129137
130138```cpp
131139// Server
132140
133- // create server socket
134- UdpServer server; // add key file and cert file here for secure connection
135-
136- // bind to port 54321 on IP 0.0.0.0
137- server.bind(54321);
138-
139- // following not needed for unsecure connection but is needed for DTLS connection
140- server.accept();
141+ // create server socket bound to all IP and 54321 port
142+ UdpServer server(54321);
141143```
142144
143145``` cpp
144146// Client
145147
146148// Connect to UDP server on IP 127.0.0.1 and port 54321
147- UDPClient client ("127.0.0.1", 54321); // add key file and cert file here for secure connection
149+ UDPClient client ("127.0.0.1", 54321);
148150```
149151
150152## Thread Safety
151153
152- 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.
154+ Do not share any of the socket objects across threads unless you provide your own thread safety on the send/read and accept calls.
153155
154156## Installation
155157
0 commit comments