-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclient_socket_boost_size.cpp
More file actions
90 lines (71 loc) · 2.77 KB
/
client_socket_boost_size.cpp
File metadata and controls
90 lines (71 loc) · 2.77 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include "example.pb.h"
#include <chrono>
#include <boost/asio.hpp>
#include <boost/array.hpp>
// nc localhost 2389
// nc -l localhost 2389
// g++ client_socket_boost_size.cpp example.pb.cc example.pb.h `pkg-config --cflags --libs protobuf`
int main()
{
auto const address = boost::asio::ip::make_address("127.0.0.1");
auto const port = static_cast<unsigned short>(std::atoi("2389"));
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket socket(io_service);
//connection
boost::system::error_code ec;
socket.connect( boost::asio::ip::tcp::endpoint( address, port ),ec);
if(ec){std::cout<<ec.message()<<std::endl; return 1;}
auto start = std::chrono::system_clock::now();
int cnt = 0;
while (cnt < 100000)
{
cnt ++;
Person per;
per.set_id(5000);
per.set_number(5000);
per.set_anotherid(5000);
per.set_aid(5000);
per.set_bid(5000);
per.set_cid(5000);
per.set_did(5000);
per.set_eid(5000);
per.set_fid(5000);
per.set_gid(5000);
size_t size = per.ByteSizeLong();
//std::cout<<size<<std::endl; // 30 bytes
void *buffer = malloc(size);
bool flag = per.SerializeToArray(buffer, size);
if(flag == false)
std::cout<<"serialize problem"<<std::endl;
boost::system::error_code error;
boost::asio::write( socket, boost::asio::buffer(buffer, size), error );
if(error)
{
std::cout << "send failed: " << error.message() << std::endl;
}
// getting response from server
boost::asio::streambuf receive_buffer;
boost::asio::read(socket, receive_buffer,boost::asio::transfer_exactly(size), error);
if( error ) {
std::cout << "receive failed: " << error.message() << std::endl;
}
else {
const char* data = boost::asio::buffer_cast<const char*>(receive_buffer.data());
Person per2;
per2.ParseFromArray(data, size);
// std::cout<<per2.id()<<" per2.id()"<<std::endl;
// std::cout<<per2.number()<<" per2.number()"<<std::endl;
// std::cout<<per2.aid()<<" per2.aid()"<<std::endl;
// std::cout<<per2.bid()<<" per2.bid()"<<std::endl;
// std::cout<<per2.cid()<<" per2.cid()"<<std::endl;
// std::cout<<per2.did()<<" per2.did()"<<std::endl;
// std::cout<<per2.eid()<<" per2.eid()"<<std::endl;
// std::cout<<per2.fid()<<" per2.fid()"<<std::endl;
}
}
auto end = std::chrono::system_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << elapsed.count() << '\n';
return 0;
}