Blame view

examples/unix/unecho.cpp 1.21 KB
48b4c725   Peter M. Groen   Setting up Socket-pp
1
2
  #include <iostream>
  #include <string>
18a2dbfb   Peter M. Groen   Fixed paths
3
4
  #include "socket-cpp/unix_connector.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;
  
  // --------------------------------------------------------------------------
  
  int main(int argc, char* argv[])
  {
  	cout << "Sample Unix-domain echo client for 'sockpp' "
18a2dbfb   Peter M. Groen   Fixed paths
13
  		<< osdev::components::socket-cpp::SOCKPP_VERSION << '\n' << endl;
48b4c725   Peter M. Groen   Setting up Socket-pp
14
15
16
  
  	string path = (argc > 1) ? argv[1] : "/tmp/unechosvr.sock";
  
18a2dbfb   Peter M. Groen   Fixed paths
17
  	osdev::components::socket-cpp::socket_initializer sockInit;
48b4c725   Peter M. Groen   Setting up Socket-pp
18
  
18a2dbfb   Peter M. Groen   Fixed paths
19
  	osdev::components::socket-cpp::unix_connector conn;
48b4c725   Peter M. Groen   Setting up Socket-pp
20
  
18a2dbfb   Peter M. Groen   Fixed paths
21
      bool ok = conn.connect(osdev::components::socket-cpp::unix_address(path));
48b4c725   Peter M. Groen   Setting up Socket-pp
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  	if (!ok) {
  		cerr << "Error connecting to UNIX socket at " << path
  			<< "\n\t" << conn.last_error_str() << endl;
  		return 1;
  	}
  
  	cout << "Created a connection to '" << conn.peer_address() << "'" << endl;
  
  	string s, sret;
  	while (getline(cin, s) && !s.empty()) {
  		if (conn.write(s) != (int) s.length()) {
  			cerr << "Error writing to the UNIX stream" << endl;
  			break;
  		}
  
  		sret.resize(s.length());
  		int n = conn.read_n(&sret[0], s.length());
  
  		if (n != (int) s.length()) {
  			cerr << "Error reading from UNIX stream" << endl;
  			break;
  		}
  
  		cout << sret << endl;
  	}
  
  	return (!conn) ? 1 : 0;
  }