-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
52 lines (45 loc) · 1.41 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
#include <boost/asio.hpp>
#include <iostream>
int main()
{
boost::asio::io_service service;
// Create a socket and connect it to the server
boost::asio::ip::tcp::socket socket(service);
socket.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 3320));
// At this point, you can communicate with the server using the socket.
std::cout << "Connected to server at " << socket.remote_endpoint() << std::endl;
bool close_connection_flag = false;
while (true)
{
std::cout<<"Please input an integer: ";
std::string mes = "";
std::cin >> mes;
if (mes == "close")
{
socket.close();
break;
}
else
{
bool input_check_flag = true;
for (int i = 0; i < mes.size(); i++)
{
if (!isdigit(mes[i]))
{
input_check_flag = false;
std::cout<<"please input integer"<<std::endl;
break;
}
}
if (input_check_flag)
{
int index_query = stoi(mes);
boost::asio::write(socket, boost::asio::buffer(&index_query,sizeof(int)));
std::cout << "Sent message to server: " <<index_query<< std::endl;
long long int reply;
boost::asio::read(socket, boost::asio::buffer(&reply, sizeof(long long int)), boost::asio::transfer_exactly(sizeof(long long int)));
std::cout << "Receive message from server: " << reply << std::endl<<std::endl;
}
}
}
}