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:";
}
}