Boost Sockets

I am trying to build a class which handles all the connections to a certain server
the problem is when i try to use the socket on another function (on the constructor it works great..)
for some reason i can't use the socket in the Recive() function

my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class defcon{
private:
	std::string m_host;
	std::string m_port;
	
	tcp::socket* socket;
public:
defcon(std::string host, std::string port);
void recive();

}
	// Constructor
	defcon::defcon(std::string host, std::string port):
		m_host(host), m_port(port)
	{
		boost::asio::io_service io_service;
		tcp::resolver resolver(io_service);
		tcp::resolver::query query(this->m_host, this->m_port);
		tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

		tcp::socket* socket = new tcp::socket(io_service);
		this->socket = socket;
		boost::asio::connect((*socket), endpoint_iterator);
		
// this code works great in the constructor:
		boost::array<char, 128> buf;
		boost::system::error_code error;
		for (;;)
		{
			size_t len = this->socket->read_some(boost::asio::buffer(buf), error);
			std::cout.write(buf.data(), len);
		}
		
	}

	void defcon::recive()
	{
// same code not working in the recive
		boost::array<char, 128> buf;
		boost::system::error_code error;

		size_t len = this->socket->read_some(boost::asio::buffer(buf), error);
		std::cout.write(buf.data(), len);
	}
Last edited on
Solved!

Just needed to make the variable
boost::asio::io_service io_service;

as class member since the socket needs it
Topic archived. No new replies allowed.