您好我正在尝试使用Boost C ++ UDP客户端服务器。
我采用了ASIO附带的一个示例并对其进行了一些修改。
它在本地网络上运行良好但是当我将服务器托管到我们的一台服务器并且端口正确转发时,它无法正常工作。
我运行UDP服务器并尝试通过互联网从家中传输客户端,但没有任何数据包到达服务器。我确信端口正确转发,并且防火墙不会妨碍。
就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;
}
答案 0 :(得分:3)
从传输层的角度来看,本地网络和互联网之间的UDP没有区别。但是,有各种因素可以发挥作用:
在大多数情况下,如果客户端和服务器在LAN上工作,那么它通常是一个网络问题。使用基本工具(如netcat)和使用可靠的基于连接的协议(如TCP)调试网络可能是值得的。一旦观察到连接,然后交换到使用UDP客户端和服务器。