Commit 991e8946 authored by Davis King's avatar Davis King

Made these examples more robust. Now if the server's start() function throws an

exception it will be printed right away rather than after the user hits
the enter key.  I also removed the global server objects since that was
pretty sketchy too.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403713
parent b9f602e1
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "dlib/server.h" #include "dlib/server.h"
#include "dlib/ref.h" // for ref()
using namespace dlib; using namespace dlib;
using namespace std; using namespace std;
...@@ -91,28 +92,42 @@ class web_server : public server::http_1a_c ...@@ -91,28 +92,42 @@ class web_server : public server::http_1a_c
}; };
// create an instance of our web server void thread(web_server& the_server)
web_server our_web_server;
void thread()
{ {
cout << "Press enter to end this program" << endl; try
cin.get(); {
// this will cause the server to shut down which will in turn cause // Start the server. start() blocks until the server is shutdown
// our_web_server.start() to unblock and thus the main() function will terminate. // by a call to clear()
our_web_server.clear(); the_server.start();
}
catch (socket_error& e)
{
cout << "Socket error while starting server: " << e.what() << endl;
}
catch (exception& e)
{
cout << "Error while starting server: " << e.what() << endl;
}
} }
int main() int main()
{ {
try try
{ {
// create a thread that will listen for the user to end this program // create an instance of our web server
thread_function t(thread); web_server our_web_server;
// make it listen on port 5000 // make it listen on port 5000
our_web_server.set_listening_port(5000); our_web_server.set_listening_port(5000);
our_web_server.start();
// create a thread that will start the server. The ref() here allows us to pass
// our_web_server into the threaded function by reference.
thread_function t(thread, ref(our_web_server));
cout << "Press enter to end this program" << endl;
cin.get();
// this will cause the server to shut down
our_web_server.clear();
} }
catch (exception& e) catch (exception& e)
{ {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "dlib/sockets.h" #include "dlib/sockets.h"
#include "dlib/server.h" #include "dlib/server.h"
#include "dlib/ref.h" // for ref()
#include <iostream> #include <iostream>
using namespace dlib; using namespace dlib;
...@@ -41,34 +42,44 @@ class serv : public server::kernel_1a_c ...@@ -41,34 +42,44 @@ class serv : public server::kernel_1a_c
}; };
serv our_server;
void thread() void thread(serv& our_server)
{ {
cout << "Press enter to end this program" << endl; try
cin.get(); {
// this will cause the server to shut down which will in turn cause // Start the server. start() blocks until the server is shutdown
// our_server.start() to unblock and thus the main() function will terminate. // by a call to clear()
our_server.clear(); our_server.start();
}
catch (socket_error& e)
{
cout << "Socket error while starting server: " << e.what() << endl;
}
catch (exception& e)
{
cout << "Error while starting server: " << e.what() << endl;
}
} }
int main() int main()
{ {
try try
{ {
// create a thread that will listen for the user to end this program serv our_server;
thread_function t(thread);
// set up the server object we have made // set up the server object we have made
our_server.set_listening_port(1234); our_server.set_listening_port(1234);
our_server.set_max_connections(1000); our_server.set_max_connections(1000);
// start the server // create a thread that will start the server. The ref() here allows us to pass
our_server.start(); // our_server into the threaded function by reference.
thread_function t(thread, ref(our_server));
cout << "Press enter to end this program" << endl;
cin.get();
// this will cause the server to shut down
our_server.clear();
} }
catch (exception& e) catch (exception& e)
{ {
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "dlib/sockets.h" #include "dlib/sockets.h"
#include "dlib/server.h" #include "dlib/server.h"
#include "dlib/sockstreambuf.h" #include "dlib/sockstreambuf.h"
#include "dlib/ref.h"
#include <iostream> #include <iostream>
using namespace dlib; using namespace dlib;
...@@ -67,32 +68,44 @@ class serv : public server::kernel_1a_c ...@@ -67,32 +68,44 @@ class serv : public server::kernel_1a_c
}; };
serv our_server;
void thread() void thread(serv& our_server)
{ {
cout << "Press enter to end this program" << endl; try
cin.get(); {
// this will cause the server to shut down which will in turn cause // Start the server. start() blocks until the server is shutdown
// our_server.start() to unblock and thus the main() function will terminate. // by a call to clear()
our_server.clear(); our_server.start();
}
catch (socket_error& e)
{
cout << "Socket error while starting server: " << e.what() << endl;
}
catch (exception& e)
{
cout << "Error while starting server: " << e.what() << endl;
}
} }
int main() int main()
{ {
try try
{ {
// create a thread that will listen for the user to end this program serv our_server;
thread_function t(thread);
// set up the server object we have made // set up the server object we have made
our_server.set_listening_port(1234); our_server.set_listening_port(1234);
our_server.set_max_connections(1000); our_server.set_max_connections(1000);
// start the server // create a thread that will start the server. The ref() here allows us to pass
our_server.start(); // our_server into the threaded function by reference.
thread_function t(thread, ref(our_server));
cout << "Press enter to end this program" << endl;
cin.get();
// this will cause the server to shut down
our_server.clear();
} }
catch (exception& e) catch (exception& e)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment