Почему сие зависает (и сервер, и клиент) при попытке клиента подключится? Server:
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using boost::asio::ip::tcp;
using namespace boost::archive;
using namespace std;
class rpc_function
{
public:
virtual void load(text_iarchive& arr,const unsigned version)=0;
virtual void save(text_oarchive& arr,const unsigned version)const=0;
BOOST_SERIALIZATION_SPLIT_MEMBER();
virtual void operator()()=0;
};
class hello_world_func:public rpc_function
{
string res;
public:
void load(text_iarchive& arr,const unsigned version)
{
}
void save(text_oarchive& arr,const unsigned version)const
{
arr << res;
}
void operator()()
{
res="Hello, World!\n";
}
};
const size_t func_cnt=1;
rpc_function* funcs[func_cnt];
struct func_data
{
unsigned func_id;
template<class archive>
void load(archive& arr,const unsigned version)
{
arr >> func_id;
arr >> (*funcs[func_id]);
}
template<class archive>
void save(archive& arr,const unsigned version)const
{
// arr func_id;
arr << (*funcs[func_id]);
}
BOOST_SERIALIZATION_SPLIT_MEMBER();
void operator()()
{
(*funcs[func_id])();
}
};
int main()
{
funcs[0]=new hello_world_func();
try
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 31005);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
acceptor.accept(*stream.rdbuf());
func_data fd;
text_iarchive ar(stream);
ar>>fd;
fd();
text_oarchive ar2(stream);
ar2<<fd;
stream.close();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using boost::asio::ip::tcp;
using namespace boost::archive;
using namespace std;
class func_data
{
unsigned int fdata;
string res;
public:
func_data(unsigned int _fdata){fdata=_fdata;}
template <typename Archive>
void load(Archive& arr,const unsigned int version)
{
arr >> res;
}
template <typename Archive>
void save(Archive& arr,const unsigned int version)const
{
arr << fdata;
}
void printRes(){cout<<res;}
BOOST_SERIALIZATION_SPLIT_MEMBER();
};
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: client2 <host>" << std::endl;
return 1;
}
tcp::iostream stream(argv[1], "31005");
/*std::string line;
std::getline(s, line);
std::cout << line << std::endl;*/
func_data fd(0);
{
text_oarchive oar(stream);
oar<<fd;
text_iarchive iar(stream);
iar>>fd;
}
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}