MCPcopy Create free account
hub / github.com/dermesser/libsocket / main

Function main

examples++/echo_server.cpp:9–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7// simply receives a message and sends an answer.
8
9int main(void) {
10 using std::string;
11
12 string host = "localhost";
13 string port = "1234";
14
15 string answer("Hello back from the server!");
16 string from;
17 string fromport;
18 string buf;
19
20 buf.resize(32);
21
22 try {
23 libsocket::inet_dgram_server srv(host, port, LIBSOCKET_BOTH);
24
25 for (;;) {
26 srv.rcvfrom(buf, from, fromport);
27
28 std::cout << "Datagram from " << from << ":" << fromport << " "
29 << buf << std::endl;
30
31 srv.sndto(answer, from, fromport);
32 }
33
34 // libsocket::inet_dgram_server also has a destructor doing this for us, so we are doing explicitly and can reuse the socket.
35 srv.destroy();
36 } catch (const libsocket::socket_exception& exc) {
37 std::cerr << exc.mesg;
38 }
39
40 return 0;
41}

Callers

nothing calls this directly

Calls 3

destroyMethod · 0.80
rcvfromMethod · 0.45
sndtoMethod · 0.45

Tested by

no test coverage detected