-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransmit.cpp
More file actions
101 lines (80 loc) · 2.85 KB
/
Copy pathtransmit.cpp
File metadata and controls
101 lines (80 loc) · 2.85 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
91
92
93
94
95
96
97
98
99
100
101
#include <random>
#include <vector>
#include <iostream>
#include <boost/asio.hpp>
#include <nlohmann/json.hpp>
using namespace boost::asio;
using json = nlohmann::json;
#define TRANSMITTER_IP "192.168.56.101"
#define LISTENER_IP "192.168.56.103"
#define LISTENER_PORT 8888
#define TRANSACTIONS 1000
#define MIN 1
#define MAX 10000
double generateRandomFloat(int min, int max) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dis(min, max);
double randomFloat = dis(gen);
return round(randomFloat * 100) / 100;
}
int main() {
// Transaction data
std::vector<json> transactions;
// Generate the transactions
for (int i = 1; i <= TRANSACTIONS; i++) {
json transaction = {
{"jsonrpc", "2.0"},
{"method", "sendTransaction"},
{"params", {{
"from", TRANSMITTER_IP,
"to", LISTENER_IP,
"amount", generateRandomFloat(MIN, MAX)
}}},
{"id", i}
};
transactions.push_back(transaction);
}
// Create the I/O service
io_service io_service;
ip::tcp::resolver resolver(io_service);
ip::tcp::resolver::query query(LISTENER_IP, std::to_string(LISTENER_PORT));
std::cout << "\nTransmitting " << TRANSACTIONS << " transactions to " << LISTENER_IP << ":" << LISTENER_PORT << std::endl << std::endl;
// Connect to the listener and transmit each transaction
for (const auto& transaction : transactions) {
boost::system::error_code ec;
ip::tcp::socket socket(io_service);
ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, ec);
if (ec) {
std::cerr << "Failed to Resolve Query: " << ec.message() << "\n\n";
exit(EXIT_FAILURE);
}
// Connect to the listener
boost::asio::connect(socket, endpoint_iterator, ec);
if (ec) {
std::cerr << "Failed to Connect: " << ec.message() << "\n\n";
exit(EXIT_FAILURE);
}
// Send the JSON-RPC transaction data
std::string transaction_data = transaction.dump() + "\n";
std::cout << "Sending: " << transaction_data;
boost::asio::write(socket, boost::asio::buffer(transaction_data), ec);
if (ec) {
std::cerr << "Failed to Write Data: " << ec.message() << "\n\n";
exit(EXIT_FAILURE);
}
// Shut down the socket
socket.shutdown(ip::tcp::socket::shutdown_both, ec);
if (ec) {
std::cerr << "Failed to Shutdown Socket: " << ec.message() << "\n\n";
exit(EXIT_FAILURE);
}
// Close the socket
socket.close(ec);
if (ec) {
std::cerr << "Failed to Close Socket: " << ec.message() << "\n\n";
exit(EXIT_FAILURE);
}
}
return 0;
}