0
votes

I am writing simple socket class. I am trying to write socket according to example http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/chat/chat_client.cpp

I can connect to server (I see it in logs) but I can not send or receive message. In wireshark I do not see outgoing messages but I can see incoming messages. As I understand io_service is running in different thread and it should be possible to receive message or send message.

(It is incomplete class because I stopped at this described problem)

Ioservice is defined outside class (main)

#include <iostream>
#include <thread>
#include <boost/thread/thread.hpp>
#include "TCP/TCPSocket.h"

using namespace std;

int main()
{
    Traces::SetTraceFolder("trace");

    boost::asio::io_service io_service;
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));



    TCPSocket socket("192.168.0.7", "6000", io_service);

    char *c = new char[100];


    while (true) {

        std::cin >> *c;
        socket.WriteMessage(c);

    }

    delete [] c;
    return 0;
}

My class:

include "TCPSocket.h"

TCPSocket::TCPSocket(const std::string &adress, const std::string &port, boost::asio::io_service & serviceio)
                     : io_service(serviceio),
                       socket_(io_service),
                       resolver(io_service)

{
 Traces() << "\n" << "LOG: TCPSocket::TCPSocket(const std::string &adress, const std::string &port)";


 tcp::resolver::query query(adress, port);
 querywsk = new tcp::resolver::query(" ", " ");



 *querywsk = query;
 iterator = resolver.resolve(*querywsk);

  boost::asio::async_connect(socket_, iterator,
        boost::bind(&TCPSocket::HandleConnect, this,
        boost::asio::placeholders::error));

}

void TCPSocket::HandleConnect(const boost::system::error_code& error)
{
  Traces() << "\n" << "LOG: void TCPSocket::HandleConnect(const boost::system::error_code& error)";

  if (!error)
  {
     boost::asio::async_read(socket_,
        boost::asio::buffer(data, std::strlen(data)),
        boost::bind(&TCPSocket::HandleConnect, this,
          boost::asio::placeholders::error));
  } else
  {
        Traces() << "\n" << "ERR:";
  }
}


void TCPSocket::WriteMessage(char *dataToSend)
{
    io_service.post(boost::bind(&TCPSocket::Write, this, dataToSend));
}

void TCPSocket::Write(char *dataToSend)
{
    Traces() << "\n" << "LOG: void TCPSocket::Write(char *dataToSend)";

    data = dataToSend;

    boost::asio::async_write(socket_,
        boost::asio::buffer(data, std::strlen(data)),
        boost::bind(&TCPSocket::HandleWrite, this,
          boost::asio::placeholders::error));

}

void TCPSocket::HandleWrite(const boost::system::error_code& error)
{
  Traces() << "\n" << "LOG: void TCPSocket::HandleWrite(const boost::system::error_code& error)";

  if (!error)
  {

      boost::asio::async_write(socket_,
          boost::asio::buffer(data,
            100),
          boost::bind(&TCPSocket::HandleWrite, this,
            boost::asio::placeholders::error));
  }
  else
  {
        Traces() << "\n" << "ERR:";
  }
}
1

1 Answers

2
votes
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

At this early point in your code, the io_service has no work to do.

So the thread starts up, the io_service starts up in the thread, sees there is nothing to do and so stops immediatly.

Thus nothing happens.

You must start the io_service AFTER you have setup the work.


There are other serious problems with your code e.g. you seem a bit confused between a socket server, which listens for connection requests, and a connection which receives and transmits messages.

I suggest that you start fresh by working through the tutorial examples at http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/tutorial.html Once you have worked your way through these, you will have a much better understanding of what goes on.