2009-02-17 09:45:57 +08:00
|
|
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
|
2008-05-02 22:19:38 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-08 22:32:13 +08:00
|
|
|
#include <dlib/sockets.h>
|
|
|
|
#include <dlib/server.h>
|
2008-05-02 22:19:38 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace dlib;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-08 10:41:50 +08:00
|
|
|
class serv : public server
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2010-07-02 08:26:59 +08:00
|
|
|
serv our_server;
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
// set up the server object we have made
|
|
|
|
our_server.set_listening_port(1234);
|
2012-09-01 09:48:34 +08:00
|
|
|
// Tell the server to begin accepting connections.
|
|
|
|
our_server.start_async();
|
2008-05-02 22:19:38 +08:00
|
|
|
|
2010-07-02 08:26:59 +08:00
|
|
|
cout << "Press enter to end this program" << endl;
|
|
|
|
cin.get();
|
2008-05-02 22:19:38 +08:00
|
|
|
}
|
|
|
|
catch (exception& e)
|
|
|
|
{
|
|
|
|
cout << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|