您好,我正在尝试使用 Boost C++ UDP 客户端-服务器。
我采用了 ASIO 附带的示例之一并对其进行了一些修改。
它在本地网络上运行良好,但是当我将服务器托管到我们的一台服务器并正确转发端口时,它不起作用。
我运行 UDP 服务器并尝试通过 Internet 从家里与客户端进行传输,但没有任何数据包到达服务器。我确信端口被正确转发并且防火墙没有妨碍。
就UDP通信而言,本地网络和互联网之间有什么区别吗?
以下是客户端和服务器的代码:
- - 客户 - -
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
enum { max_length = 1024 };
int main(int argc, char* argv[])
{
char request[max_length];
try
{
boost::asio::io_service io_service;
udp::socket s(io_service, udp::endpoint(udp::v4(), 0));
udp::resolver resolver(io_service);
udp::resolver::query query(udp::v4(), "MyPublicIp", "3002");
udp::resolver::iterator iterator = resolver.resolve(query);
using namespace std; // For strlen.
std::cout << "Write to me: ";
while(1)
{
std::cin.getline(request, max_length);
size_t request_length = strlen(request);
s.send_to(boost::asio::buffer(request, request_length), *iterator);
char reply[max_length];
udp::endpoint sender_endpoint;
size_t reply_length = s.receive_from(boost::asio::buffer(reply, max_length), sender_endpoint);
std::cout << Say:";
std::cout.write(reply, reply_length);
std::cout << "\n";
std::cout << Say What?" ;
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
std::cin.getline(request, max_length);
return 0;
}
- - - 服务器 - - - - - -
#include <boost/chrono.hpp>
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost\thread\thread.hpp>
#include <ctime>
using boost::asio::ip::udp;
class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
socket_(io_service, udp::endpoint( boost::asio::ip::address_v4::any(), port))
{
std::fill(data_, data_ + max_length, 0);
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_receive_from(const boost::system::error_code& error,
size_t bytes_recvd)
{
if (!error && bytes_recvd > 0)
{
td::cout << "Connection from: " << sender_endpoint_.address().to_string() << " " << sender_endpoint_.port() << std::endl;
socket_.async_send_to(
boost::asio::buffer(data_, strlen(data_)), sender_endpoint_,
boost::bind(&server::handle_send_to, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
void handle_send_to(const boost::system::error_code& /*error*/,
size_t /*bytes_sent*/)
{
std::fill(data_, data_ + max_length, 0);
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
boost::asio::io_service& io_service_;
udp::socket socket_;
udp::endpoint sender_endpoint_;
enum { max_length = 300 };
char data_[max_length];
};
int main(int argc, char* argv[])
{
try
{
std::cout << "Starting server" << std::endl;
boost::asio::io_service io_service;
short port = 3002;
using namespace std;
server s(io_service, port);
std::cout << "Server started. Listening on port:" <<port << std::endl;
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}