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,
|
|
|
|
server and sockstreambuf 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 text it receives but in upper case.
|
|
|
|
So basically it is the same as the other sockets example except it
|
|
|
|
uses stream buffers.
|
|
|
|
|
|
|
|
To test it out you can just open a command prompt and type:
|
|
|
|
telnet localhost 1234
|
|
|
|
|
|
|
|
Then you can type away.
|
|
|
|
|
|
|
|
|
|
|
|
Also note that a good reference on the standard C++ iostream library can be
|
|
|
|
found at http://www.cplusplus.com/ref/iostream/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "dlib/sockets.h"
|
|
|
|
#include "dlib/server.h"
|
|
|
|
#include "dlib/sockstreambuf.h"
|
|
|
|
#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
|
|
|
|
)
|
|
|
|
{
|
2012-11-18 01:07:04 +08:00
|
|
|
// create a sockstreambuf that reads/writes on our connection.
|
|
|
|
sockstreambuf buf(&con);
|
2008-05-02 22:19:38 +08:00
|
|
|
|
|
|
|
// Now we make an iostream object that reads/writes to our streambuffer. A lot of people
|
|
|
|
// don't seem to know that the C++ iostreams are as powerful as they are. So what I'm doing
|
|
|
|
// here isn't anything special and is totally portable. You will be able to use this stream
|
|
|
|
// object just as you would any iostream from the standard library.
|
|
|
|
iostream stream(&buf);
|
|
|
|
|
|
|
|
// This command causes our stream to flush its output buffers whenever you ask it for more
|
|
|
|
// data.
|
|
|
|
stream.tie(&stream);
|
|
|
|
|
|
|
|
char ch;
|
2010-12-23 04:41:59 +08:00
|
|
|
// Loop until we hit the end of the stream. This happens when the connection terminates.
|
|
|
|
while (stream.peek() != EOF)
|
2008-05-02 22:19:38 +08:00
|
|
|
{
|
|
|
|
// get the next character from the client
|
|
|
|
ch = stream.get();
|
|
|
|
|
|
|
|
// now echo it back to them
|
|
|
|
stream << (char)toupper(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|