Declaring a boost asio socket, acceptor and endpoint in a class headerfile

 ̄綄美尐妖づ 提交于 2019-12-12 00:28:32

问题


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]


回答1:


The only obvious error is the rogue ; at the end of the initialiser list.

If you still have errors when you remove that, perhaps you could post a small, complete code example that demonstrates the error, and the errors themselves. Otherwise, it's rather difficult to guess what the rest of your code might be doing.



来源:https://stackoverflow.com/questions/19501167/declaring-a-boost-asio-socket-acceptor-and-endpoint-in-a-class-headerfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!