• Advertisement
Sign in to follow this  

boost asio threaded server problem

This topic is 2687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there!

I have a problem with an attempt at writing a multi threaded server with boost asio. Managed to get the server code working as server output confirms (?)

But the client returns immediately, there is no output?


#ifndef THREADED_SOCKET_H
#define THREADED_SOCKET_H

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

#include "common.h"

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

class threaded_socket
{
public:
threaded_socket(tcp::socket *socket) : m_socket(socket) {}
~threaded_socket() { m_socket->close(); }
void start()
{
m_thread = boost::thread(&threaded_socket::do_comms_stuff,this);
}

void join()
{
m_thread.join();
}
private:
void do_comms_stuff()
{
try
{
std::cout << "start..." << std::endl;

//wait 5 seconds
boost::asio::deadline_timer t(m_socket->get_io_service(),boost::posix_time::seconds(5));
t.wait();

std::cout << "middle..." << std::endl;

std::string message = make_daytime_string();
boost::system::error_code ignored_error;
//boost::asio::write(*m_socket,boost::asio::buffer(message),boost::asio::transfer_all(),ignored_error);
boost::asio::async_write
(
*m_socket,
boost::asio::buffer(message),
boost::bind
(
&threaded_socket::not_used,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);

std::cout << "end..." << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void not_used(const boost::system::error_code &,size_t) {}
tcp::socket *m_socket;
boost::thread m_thread;
};

void threaded_socket_test()
{
unsigned int count = 0;
try
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service,tcp::endpoint(tcp::v4(),13));
tcp::socket socket(io_service);

while(1)
{
std::cout << "attempting to establish connection: count = " << ++count << std::endl;

acceptor.accept(socket);

//pass off to threaded_socket instance
threaded_socket t_sock(&socket);
t_sock.start();

std::cout << "got connection, passed off to threaded socket instance." << std::endl;
}
}
catch(std::exception &e)
{
std::cout << e.what() << std::endl;
}
}

#endif


Is the problem on the client side then? The code is below.


#ifndef CLIENT_H
#define CLIENT_H

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

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

void client(std::string host)
{
try
{
boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(host, "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);

for (;;)
{
boost::array<char, 128> buf;
boost::system::error_code error;

size_t len = socket.read_some(boost::asio::buffer(buf), 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.

std::cout.write(buf.data(), len);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

#endif

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement