"git@developer.sourcefind.cn:change/sglang.git" did not exist on "637bfee448a8057fb4223f4da72bdb7467ce3712"
sockets_ex.cpp 1.26 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
4
5
6
7
8
9
10
11
12
13
14
/*

    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.  

*/




15
16
#include <dlib/sockets.h>
#include <dlib/server.h>
17
18
19
20
21
22
23
#include <iostream>

using namespace dlib;
using namespace std;



24
class serv : public server
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
    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
    {
48
        serv our_server;
49
50
51

        // set up the server object we have made
        our_server.set_listening_port(1234);
52
53
        // Tell the server to begin accepting connections.
        our_server.start_async();
54

55
56
        cout << "Press enter to end this program" << endl;
        cin.get();
57
58
59
60
61
62
63
    }
    catch (exception& e)
    {
        cout << e.what() << endl;
    }
}