echo_client.cpp
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "socket.h"
#include "config.h"
#include <memory>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <string>
void str_echo(std::istream& in, lemoce::ClientSocket & client)
{
std::string payload;
while(std::getline(in, payload))
{
try
{
std::cout << "li pelo teclado " << payload << std::endl;
client.write(payload);
std::unique_ptr<std::string> ptrRead = client.read();
std::cout << "recebi do servidor " << *ptrRead << std::endl;
}
catch(lemoce::SocketException& ex)
{
if (ex.getErrno() != EINTR)
throw ex;
}
}
}
int main(int argc, char *argv[])
{
int MAX_BUFFER = 8192;
std::string host = "localhost";
int port = 7;
//std::cin.exceptions(std::ios_base::failbit | std::ios_base::eofbit);
if (argc == 3)
{
std::istringstream issHost{argv[1]};
issHost >> host;
std::istringstream issPort{argv[2]};
issPort >> port;
}
try
{
lemoce::ClientSocket client(host, port);
client.connect();
str_echo(std::cin, client);
client.close();
}
catch (lemoce::SocketException& ex)
{
std::cout << ex.getMessage() << std::endl;
}
return 0;
}