我正在使用 boost 套接字和 boost 线程。(同步套接字)。如果我理解正确,这个方案是行不通的。
using boost::asio::ip::tcp;
boost::asio::ip::tcp::socket *MySocket;
bool Key = true;
void Function1()
{
try
{
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
boost::system::error_code error;
boost::asio::ip::address address = boost::asio::ip::address::from_string(IP, error);
tcp::endpoint ep(address, 1235);
MySocket = new tcp::socket(io_service);
MySocket->connect(ep);
Key = false;
}
catch(std::exception e)
{
std::cout<<e.what;
}
}
void Function2()
{
boost::array <char, 8> buf;
size_t len = MySocket->read_some(boost::asio::buffer(buf), error);
//Always len == 0 and buf.data() is MMMMMMM
}
void main()
{
boost::thread Thread(&Function1);
while(Key);
Function2();
MySocket->close();
}