I have a TCP/IP server made with boost asio that is wrapped in a class. Now i want declare the socket, eindpoint and acceptor in the class headerfile so that i can make memberfunctions that use the socket. The server runs now in the class constructor. I tried to use a initialiser list like below:
CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber)
: Sock(new boost::asio::ip::tcp::socket(IOService));
{
// Constructor code
But this gives compiler errors. Sock is declared in the header like this:
SmartSocket Sock;
And SmartSocket is a shared pointer that is defined the following way.
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SmartSocket;
Can anybody tell me is it is possible to declare a socket, acceptor and endpoint in the headerfile and intialize these objects?
Any suggestions are welcome!
Update
Below is te class headerfile code and the constructor code:
Header file with Endpoint, socket and acceptor declarations:
public:
// Constructor and destructor
CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber);
~CommunicationModuleTCPIP();
int Connect();
int Disconnect();
int SendData(SmartSocket sock);
int RecieveData(SmartSocket sock);
int Send(SmartSocket Sock);
int Recieve(SmartSocket sock);
int CheckConnection();
char SendBuffer[20];
char RecvBuffer[20];
SmartSocket Sock;
tcp::endpoint Endpoint;
boost::asio::ip::tcp::acceptor Acceptor;
Constructor, in comments are the socket, endpoint and acceptor when i create them in the constructor. This works but i want to make these objects part of the class so that i can use them in member functions in other part of the code (if this is possible?).
CommunicationModuleTCPIP::CommunicationModuleTCPIP(bool Server,
string IPAdress,
int PortNumber){
:Endpoint(ip::address::from_string(IPAdress),PortNumber),
Acceptor(IOService, Endpoint),
Sock(new boost::asio::ip::tcp::socket(IOService)
if(Server){
cout << "Setting up server" << endl;
//ip::tcp::endpoint Endpoint(ip::address::from_string(IPAdress),PortNumber);
// Create acceptor
//boost::asio::ip::tcp::acceptor Acceptor(IOService, Endpoint);
// Create socket
//SmartSocket Sock(new boost::asio::ip::tcp::socket(IOService));
cout << "Before accept..." << endl;
// Waiting for client
Acceptor.accept(*Sock);
cout << "Server set up" << endl;
}
The compiller errors that i get are as followed:
\CommunicationModuleTCPIP.cpp:6:30: fout: no matching function for call to ‘boost::asio::basic_socket_acceptor::basic_socket_acceptor()’
fout: expected primary-expression before ‘:’ token ..\CommunicationModuleTCPIP.cpp:7:2: fout: expected ‘;’ before ‘:’ token ..\CommunicationModuleTCPIP.cpp:199:2: fout: expected ‘}’ at end of input
C:\boost_1_54_0/boost/asio/error.hpp:244:45: let op: ‘boost::asio::error::system_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:246:45: let op: ‘boost::asio::error::netdb_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:248:45: let op: ‘boost::asio::error::addrinfo_category’ defined but not used [-Wunused-variable] C:\boost_1_54_0/boost/asio/error.hpp:250:45: let op: ‘boost::asio::error::misc_category’ defined but not used [-Wunused-variable]