-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathecho_client.cpp
56 lines (47 loc) · 1.37 KB
/
echo_client.cpp
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
/**
* @file echo_client.cpp
* @author Yukun J
* @expectation this is the demo echo client for illustration and test purpose
* @init_date Dec 26 2022
*/
#include <sys/socket.h>
#include <unistd.h>
#include <memory>
#include "core/connection.h"
#include "core/net_address.h"
#include "core/socket.h"
#include "core/thread_pool.h"
#define BUF_SIZE 2048
namespace TURTLE_SERVER {
class EchoClient {
public:
explicit EchoClient(NetAddress server_address) {
auto client_socket = std::make_unique<Socket>();
client_socket->Connect(server_address);
client_connection = std::make_unique<Connection>(std::move(client_socket));
}
void Begin() {
char buf[BUF_SIZE + 1];
memset(buf, 0, sizeof(buf));
int fd = client_connection->GetFd();
while (true) {
// scan from user keyboard
auto actual_read = read(STDIN_FILENO, buf, BUF_SIZE);
send(fd, buf, actual_read, 0);
memset(buf, 0, sizeof(buf));
// echo back to screen from server's message
auto actual_recv = recv(fd, buf, BUF_SIZE, 0);
write(STDOUT_FILENO, buf, actual_recv);
memset(buf, 0, sizeof(buf));
}
}
private:
std::unique_ptr<Connection> client_connection;
};
} // namespace TURTLE_SERVER
int main() {
TURTLE_SERVER::NetAddress local_address("0.0.0.0", 20080);
TURTLE_SERVER::EchoClient echo_client(local_address);
echo_client.Begin();
return 0;
}