diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 02:57:58 +0000 |
commit | be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 (patch) | |
tree | 9754ff1ca740f6346cf8483ec915d4054bc5da2d /ml/dlib/examples/sockets_ex.cpp | |
parent | Initial commit. (diff) | |
download | netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.tar.xz netdata-be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97.zip |
Adding upstream version 1.44.3.upstream/1.44.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ml/dlib/examples/sockets_ex.cpp')
-rw-r--r-- | ml/dlib/examples/sockets_ex.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/ml/dlib/examples/sockets_ex.cpp b/ml/dlib/examples/sockets_ex.cpp new file mode 100644 index 00000000..5fd9ebe0 --- /dev/null +++ b/ml/dlib/examples/sockets_ex.cpp @@ -0,0 +1,63 @@ +// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt +/* + + This is an example illustrating the use of the sockets and + server components from the dlib C++ Library. + + This is a simple echo server. It listens on port 1234 for incoming + connections and just echos back any data it receives. + +*/ + + + + +#include <dlib/sockets.h> +#include <dlib/server.h> +#include <iostream> + +using namespace dlib; +using namespace std; + + + +class serv : public server +{ + void on_connect ( + connection& con + ) + { + char ch; + while (con.read(&ch,1) > 0) + { + // we are just reading one char at a time and writing it back + // to the connection. If there is some problem writing the char + // then we quit the loop. + if (con.write(&ch,1) != 1) + break; + } + } + +}; + + +int main() +{ + try + { + serv our_server; + + // set up the server object we have made + our_server.set_listening_port(1234); + // Tell the server to begin accepting connections. + our_server.start_async(); + + cout << "Press enter to end this program" << endl; + cin.get(); + } + catch (exception& e) + { + cout << e.what() << endl; + } +} + |