Blame view

examples/tcp/tcpechosvr.cpp 1.94 KB
48b4c725   Peter M. Groen   Setting up Socket-pp
1
2
  #include <iostream>
  #include <thread>
18a2dbfb   Peter M. Groen   Fixed paths
3
4
  #include "socket-cpp/tcp_acceptor.h"
  #include "socket-cpp/version.h"
48b4c725   Peter M. Groen   Setting up Socket-pp
5
6
7
8
9
10
11
12
  
  using namespace std;
  
  // --------------------------------------------------------------------------
  // The thread function. This is run in a separate thread for each socket.
  // Ownership of the socket object is transferred to the thread, so when this
  // function exits, the socket is automatically closed.
  
18a2dbfb   Peter M. Groen   Fixed paths
13
  void run_echo(osdev::components::socket-cpp::tcp_socket sock)
48b4c725   Peter M. Groen   Setting up Socket-pp
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  {
  	ssize_t n;
  	char buf[512];
  
  	while ((n = sock.read(buf, sizeof(buf))) > 0)
  		sock.write_n(buf, n);
  
  	cout << "Connection closed from " << sock.peer_address() << endl;
  }
  
  // --------------------------------------------------------------------------
  // The main thread runs the TCP port acceptor. Each time a connection is
  // made, a new thread is spawned to handle it, leaving this main thread to
  // immediately wait for the next connection.
  
  int main(int argc, char* argv[])
  {
  	cout << "Sample TCP echo server for 'sockpp' "
18a2dbfb   Peter M. Groen   Fixed paths
32
  		<< osdev::components::socket-cpp::SOCKPP_VERSION << '\n' << endl;
48b4c725   Peter M. Groen   Setting up Socket-pp
33
34
35
  
  	in_port_t port = (argc > 1) ? atoi(argv[1]) : 12345;
  
18a2dbfb   Peter M. Groen   Fixed paths
36
  	osdev::components::socket-cpp::socket_initializer sockInit;
48b4c725   Peter M. Groen   Setting up Socket-pp
37
  
18a2dbfb   Peter M. Groen   Fixed paths
38
  	osdev::components::socket-cpp::tcp_acceptor acc(port);
48b4c725   Peter M. Groen   Setting up Socket-pp
39
40
41
42
43
44
45
46
47
  
  	if (!acc) {
  		cerr << "Error creating the acceptor: " << acc.last_error_str() << endl;
  		return 1;
  	}
      //cout << "Acceptor bound to address: " << acc.address() << endl;
  	cout << "Awaiting connections on port " << port << "..." << endl;
  
  	while (true) {
18a2dbfb   Peter M. Groen   Fixed paths
48
  		osdev::components::socket-cpp::inet_address peer;
48b4c725   Peter M. Groen   Setting up Socket-pp
49
50
  
  		// Accept a new client connection
18a2dbfb   Peter M. Groen   Fixed paths
51
  		osdev::components::socket-cpp::tcp_socket sock = acc.accept(&peer);
48b4c725   Peter M. Groen   Setting up Socket-pp
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  		cout << "Received a connection request from " << peer << endl;
  
  		if (!sock) {
  			cerr << "Error accepting incoming connection: " 
  				<< acc.last_error_str() << endl;
  		}
  		else {
  			// Create a thread and transfer the new stream to it.
  			thread thr(run_echo, std::move(sock));
  			thr.detach();
  		}
  	}
  
  	return 0;
  }