network settings for running a boost asio server on localhost

I wrote a client client.cpp and a server server.cpp and they compile well. But I don't know how to connect client.cpp to server.cpp in localhost

I wrote the codes with Boost.Asio and they compile well, but I don't know how to make the network settings so that server.cpp always waits for new connections on a specific port.

I wanted to connect the client code to the server on a specific port and get the information from the server.cpp, but I don't know the network settings.

my OS is Windows 10

client.cpp:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

int main(int argc, char* argv[]){
    
    try {

        if (argc != 3){
              std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
              return 1;
        }

        boost::asio::io_context io_context;

        tcp::resolver resolver(io_context);
        tcp::resolver::results_type endpoints =
        resolver.resolve(tcp::v4(), argv[1], argv[2]);

        tcp::socket s(io_context);
        boost::asio::connect(s, endpoints);

        using namespace std; // For strlen.
        std::cout << "Enter message: ";
        char request[max_length];
        std::cin.getline(request, max_length);
        size_t request_length = strlen(request);
        boost::asio::write(s, boost::asio::buffer(request, request_length));

        char reply[max_length];
        size_t reply_length = boost::asio::read(s,
        boost::asio::buffer(reply, request_length));
        std::cout << "Reply is: ";
        std::cout.write(reply, reply_length);
        std::cout << "\n";
   }
   catch (std::exception& e)
   {
        std::cerr << "Exception: " << e.what() << "\n";
   }

   return 0;
}

server.cpp

#include <cstdlib>
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>

using boost::asio::ip::tcp;

const int max_length = 1024;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock){

    try{

        for (;;){
            char data[max_length];

            boost::system::error_code error;
            size_t length = sock->read_some(boost::asio::buffer(data), error);
            if (error == boost::asio::error::eof)
                 break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.

            boost::asio::write(*sock, boost::asio::buffer(data, length));
        }
    }
    catch (std::exception& e){
         std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

void server(boost::asio::io_context& io_context, unsigned short port)
{
     tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port));
     for (;;){
          socket_ptr sock(new tcp::socket(io_context));
          a.accept(*sock);
          boost::thread t(boost::bind(session, sock));
     }
}

int main(int argc, char* argv[])
{
    try{

        if (argc != 2){
            std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
            return 1;
        }

        boost::asio::io_context io_context;

        using namespace std; // For atoi.
        server(io_context, atoi(argv[1]));
 }
 catch (std::exception& e)
 {
     std::cerr << "Exception: " << e.what() << "\n";
 }

return 0;
}


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations