the question is, how can I connect one program between two or more computers over a network, in other words, I want to create a dialog application on Windows in cmd (yes, the idea can be strange and stupid, but it just arouses incredible interest in me). I tried to create a connection between the host and the server, but firstly, I did not succeed (maybe I misunderstood this method), and secondly, I think, I need a host to host connection
That's all the code I came up with, in general there were many implementations of how to connect, but none of them fit or did not work
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include <boost/asio.hpp>
#include <fstream>
#include <ctime>
#include <time.h>
#include <boost/bind/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace std;
using namespace boost::asio;
using boost::asio::ip::tcp;
string message;
string nickname;
string Mess(string name, string text);
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_context& io_context)
{
return pointer(new tcp_connection(io_context));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = Mess(nickname, message);
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
tcp_connection(boost::asio::io_context& io_context)
: socket_(io_context)
{
}
void handle_write(const boost::system::error_code& /*error*/,
size_t /*bytes_transferred*/)
{
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_context& io_context)
: io_context_(io_context),
acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
{
start_accept();
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(io_context_);
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
}
start_accept();
}
boost::asio::io_context& io_context_;
tcp::acceptor acceptor_;
};
string Current_time()
{
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_sec = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
return std::ctime(&end_time);
}
string Mess(string name, string text)
{
string mes = name + ": " + text;
return mes;
}
int main()
{
boost::asio::io_context io;
boost::asio::steady_timer timer(io, boost::asio::chrono::seconds(3));
io_service serv;
string ipadress;
int start;
ip::tcp::socket socket1(serv);
ip::tcp::socket socket2(serv);
tcp_server server(serv);
serv.run();
cout << "\n\t\tYou in my Social network\n" << std::endl;
cout << "\n1 - to start chating, 2 - to exit, 3 - to check your host name: ";
cin >> start;
if (start == 1)
{
cout << "\nWrite your nickname (more than 3 symbols): ";
cin >> nickname;
if (nickname.length() <= 3)
{
system("cls");
cout << "\n\n\t\tYour nickname should be more than 3 symbols\n\n\n";
exit(1);
}
else
{
system("cls");
//cout << "ip: " << IPad() << endl;
cout << "Your nick is: '" << nickname << "'";
tcp_connection tc();
cout << "\n\nWrite your message (to end, press Ctrl + Z): ";
cin >> message;
cout << "\n" << Mess(nickname, message) << "\ttime when sended: " << Current_time() << "\n";
}
}
else if (start == 2)
{
timer.wait();
io.run();
system("cls");
exit(1);
}
else if (start == 3)
{
cout << "\n\tYour host name is: " << boost::asio::ip::host_name() << endl;
}
else
{
system("cls");
cout << "\n\n\n\t\t\t1 or 2!!!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
cout << "bye";
timer.wait();
exit(1);
}
}