-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
66 lines (56 loc) · 1.38 KB
/
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
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <iostream>
#include <string>
#include <stdlib.h>
#include<asio.hpp>
#include<asio/ts/buffer.hpp>
#include<asio/ts/internet.hpp>
#include<thread>
#define PORT 2334
#define IP "127.0.0.1"//Here change to the IP of your server as proxy.
using namespace std;
using asio::ip::tcp;
void send_mail(tcp::socket* socket, string key) {
string buffer;
asio::write(*socket, asio::buffer("S" + key + "##"));
cout << "Message: ";
//cin >> buffer;
cin.ignore();// !! Trick
getline(cin, buffer);
asio::write(*socket, asio::buffer(buffer+"#"));
}
void recv_mail(tcp::socket* socket, string key) {
string c = "R"+key + "##";
asio::error_code err;
asio::write(*socket, asio::buffer(c), err);
asio::streambuf buffer;
asio::read_until(*socket, buffer, "####");
cout << &buffer << endl;
}
int main() {
string key;
int cmd;
cout << "Keyword:\t";
cin >> key;
asio::io_context context;
tcp::endpoint endpoint(asio::ip::address::from_string(IP), PORT);
asio::error_code err;
while (1) {
cout << ">> ";
cin >> cmd;
if (cmd == 3)
break;
if (cmd == 1) {
tcp::socket socket(context);
socket.connect(endpoint, err);
send_mail(&socket, key);
socket.close();
}
if (cmd == 2) {
tcp::socket socket(context);
socket.connect(endpoint, err);
recv_mail(&socket, key);
socket.close();
}
}
}