1

I want to have a non blocking server to read the incoming data in my application.

this is the workflow i want.

void main()
{

 // create a socket server
  CreateSocket();
  while( true )
   {
      // keep doing the other tasks.
      // if we receive a message than process it
   }    
}

this is the code for the Socket connection

#pragma once
//importing libraries
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost::asio;
using ip::tcp;
using std::cout;
using std::endl;


class con_handler : public boost::enable_shared_from_this<con_handler>
{
private:
    tcp::socket sock;
    std::string message = "Hello From Server";
    enum { max_length = 2048};
    char data[max_length];

public:
    typedef boost::shared_ptr<con_handler> pointer;
    con_handler(boost::asio::io_service& io_service) : sock( io_service) {}

    // creating the pointer
    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new con_handler(io_service));
    }

    // socket creation
    tcp::socket& socket()
    {
        return sock;
    }

    void start()
    {
        sock.async_read_some(
            boost::asio::buffer(data, max_length),
            boost::bind(&con_handler::handle_read,
                shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
    }


    void handle_read(const boost::system::error_code& err, size_t bytes_transferred)
    {
        if (!err) {
            cout << data << endl;
        }
        else {
            std::cerr << "error: " << err.message() << std::endl;
            sock.close();
        }
    }
    void handle_write(const boost::system::error_code& err, size_t bytes_transferred)
    {
        if (!err) {
            cout << "Server sent Hello message!" << endl;
        }
        else {
            std::cerr << "error: " << err.message() << endl;
            sock.close();
        }
    }
};

////////////////////////////////////////////////////////

#pragma once
#include "TCP_Server.h"

class Server
{
private:
    tcp::acceptor acceptor_;
    void start_accept()
    {
        // socket
        con_handler::pointer connection = con_handler::create(acceptor_.get_io_service());

        // asynchronous accept operation and wait for a new connection.
        acceptor_.async_accept(connection->socket(),
            boost::bind(&Server::handle_accept, this, connection,
                boost::asio::placeholders::error));
    }
public:
    //constructor for accepting connection from client
    Server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234))
    {
        start_accept();
    }
    void handle_accept(con_handler::pointer connection, const boost::system::error_code& err)
    {
        if (!err) {
            connection->start();
        }
        start_accept();
    }
};

Currently the socket connection is working but it is blocking the other process.

Do i need to have a separate thread for this workflow or i can do it in the same thread.

this is my main function

#include "pch.h"
#include <iostream>
#include "Server.h"

int main()
{
        boost::asio::io_service io_service;
        Server server(io_service);
        io_service.run();

    while (true)
    {
        std::cout << " function is running Running";    
    }
}

I am able to receive input messages but i never reach the while statement.

i would want while to keep printing and the server to receive message at the same time.

  • 1
    What is being "blocked"? What is "the other process"? Can you please elaborate on what's happening and what should be happening? And please try to create a proper [mcve] to show us, including your actual `main` function and how you use the `Server` class. – Some programmer dude Oct 28 '19 at 09:12
  • @Some programmer dude i have added the main function please have a look. –  Oct 28 '19 at 09:19
  • 2
    I think you need to [read a little more about `io_service::run`](https://www.boost.org/doc/libs/1_71_0/doc/html/boost_asio/reference/io_context/run/overload1.html) and what it does. Putting it in a thread is one possible solution. – Some programmer dude Oct 28 '19 at 09:21
  • Possible duplicate of [Boost::Asio : io\_service.run() vs poll() or how do I integrate boost::asio in mainloop](https://stackoverflow.com/questions/4705411/boostasio-io-service-run-vs-poll-or-how-do-i-integrate-boostasio-in-ma) – Gizmo Oct 28 '19 at 15:00
  • You are on the right way, you only need to [learn asio](https://stackoverflow.com/questions/244453/best-documentation-for-boostasio). – Jean Davy Oct 29 '19 at 10:33

0 Answers0