我的问题是我似乎没有收到我发送的数据包,因为available()
函数总是返回0.我在Windows上使用独立版本的Asio。我不是一个完整的新手,之前曾经使用过ASIO(但我还是很可怕)。
我之前尝试过打开moveSoc,但这没有帮助。我也尝试将endPoint绑定到acceptor,但这会导致参数无效错误。
设置
asio::error_code errCode;
asio::ip::address oponentAdress = checkPoint.address(); //Adress of a valid UDP endpoint
std::cout << checkPoint.address().to_string() << std::endl;
endPoint.address(oponentAdress);
endPoint.port(portNum);
acceptor->listen();
moveSoc.connect(endPoint, errCode);
std::cout << errCode.message() << std::endl;
acceptor->accept(moveSoc, errCode);
std::cout << errCode.message() << std::endl;
std::cout << "Found Client!" << std::endl;
发送代码
void ClientHandler::Outbound()
{
moveSoc.write_some(asio::buffer(m_outgoingPacs, m_packetNum)); //Packet num is a valid number
}
阅读代码
bool ClientHandler::Inbound()
{
uint bytesToRead = moveSoc.available();
if (bytesToRead > 0)
moveSoc.read_some(asio::buffer(m_incomingPacs, bytesToRead));
return (bytesToRead > 0);
}