mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-08-04 01:36:10 -04:00
83 lines
2.1 KiB
C++
83 lines
2.1 KiB
C++
//=======================================================================
|
|
// Copyright Baptiste Wicht 2013-2016.
|
|
// Distributed under the terms of the MIT License.
|
|
// (See accompanying file LICENSE or copy at
|
|
// http://www.opensource.org/licenses/MIT)
|
|
//=======================================================================
|
|
|
|
#include <tlib/system.hpp>
|
|
#include <tlib/errors.hpp>
|
|
#include <tlib/print.hpp>
|
|
#include <tlib/net.hpp>
|
|
#include <tlib/dns.hpp>
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc != 3) {
|
|
tlib::print_line("usage: nc server port");
|
|
return 1;
|
|
}
|
|
|
|
std::string server(argv[1]);
|
|
std::string port_str(argv[2]);
|
|
auto port = std::atoui(port_str);
|
|
|
|
auto ip_parts = std::split(server, '.');
|
|
|
|
if (ip_parts.size() != 4) {
|
|
tlib::print_line("Invalid address IP for the server");
|
|
return 1;
|
|
}
|
|
|
|
auto server_ip = tlib::ip::make_address(std::atoui(ip_parts[0]), std::atoui(ip_parts[1]), std::atoui(ip_parts[2]), std::atoui(ip_parts[3]));
|
|
|
|
tlib::socket sock(tlib::socket_domain::AF_INET, tlib::socket_type::STREAM, tlib::socket_protocol::TCP);
|
|
|
|
sock.connect(server_ip, port);
|
|
sock.listen(true);
|
|
|
|
if (!sock) {
|
|
tlib::printf("nc: socket error: %s\n", std::error_message(sock.error()));
|
|
return 1;
|
|
}
|
|
|
|
tlib::tcp::packet_descriptor desc;
|
|
desc.payload_size = 4;
|
|
|
|
auto packet = sock.prepare_packet(&desc);
|
|
|
|
if (!sock) {
|
|
tlib::printf("nc: socket error: %s\n", std::error_message(sock.error()));
|
|
return 1;
|
|
}
|
|
|
|
auto* payload = reinterpret_cast<char*>(packet.payload + packet.index);
|
|
payload[0] = 'T';
|
|
payload[1] = 'H';
|
|
payload[2] = 'O';
|
|
payload[3] = 'R';
|
|
|
|
sock.finalize_packet(packet);
|
|
|
|
if (!sock) {
|
|
tlib::printf("nc: socket error: %s\n", std::error_message(sock.error()));
|
|
return 1;
|
|
}
|
|
|
|
tlib::sleep_ms(2000);
|
|
|
|
sock.listen(false);
|
|
|
|
if (!sock) {
|
|
tlib::printf("nc: socket error: %s\n", std::error_message(sock.error()));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|