I'm making a TCP server by using the boost::asio library.
At this moment, I have working it with a code like this: Note:This is a testing server for non-profit.
int main(){
const int SERVER_PORT = 60000;
try{
...
//declare the io_service, endpoint, acceptor and socket
...
{
acceptor.accept(socket);
boost::asio::write(socket, boost::asio::buffer(message));
boost::asio::streambuf received;
boost::asio::read_until(socket, received, "\r\n");
...
//all server operations
...
}
}
}
catch (std::exception& ex)
{
std::cerr << "Exception " << ex.what() << std::endl;
}
return 0;
}
Now, I wan't to improve the code and I started by organizing my code by functions like this:
void session(){
boost::asio::write(socket, boost::asio::buffer(message));
boost::asio::streambuf received;
boost::asio::read_until(socket, received, "\r\n");
...
//All server operations
...
}
void server(boost::asio::io_service& io_service, const int SERVER_PORT){
...
//declare the io_service, endpoint, acceptor and socket
...
session();//Call Session
}
int main(){
const int SERVER_PORT = 60000;
boost::asio::io_service io_service;
server(io_service, SERVER_PORT);
return 0;
}
My problem is that with the functions, it doesn't compile. I have the Visual Studio compilation error: C2228
error C2228:The operand to the left of the period .read_some is not a class, structure, or union
error C2228:The operand to the left of the period .write_some is not a class, structure, or union
I don't found how to fix this problem.
Thank you very much!
EDIT: SOLUTION BASED ON THE ANSWER
Unless socket is a global vairable, void session() is not valid. The good one is:
void session(boost::asio::ip::tcp::socket& socket)
Thanks very much to Orrkid
Where is socket defined? sounds like it isn't properly defined causing the write and read_until functions to throw the error.
read_some and write_some are called inside the write and read_until
(edit) looking at your code, unless socket is a global vairable, how is it getting passed into your session function, I see where you commented that you will declare them in your server function, but unless you have omitted more code from the example, looks like void session() is not valid.