1
此升压udp服务器不能按预期运行。为什么不能提升asio代码的正常工作?
它与封锁的UDP回应服务器EXCEPT相同,用于较小的更改。 我正在使用不同的套接字来返回响应,即sock2。 现在,如果客户端与服务器位于同一台计算机上,此代码完美工作。 但是当我在不同的机器上运行这个时候,没有收到响应。 但是,如果我将发送套接字更改为sock而不是sock2,则它可以跨机器使用 。
任何想法为什么会这样? wireshark根本没有显示任何错误。客户端使用随机源端口,但随后在同一随机端口上调用recv_from。服务器将响应发送回客户端监听的相同端口号。
#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
enum { max_length = 1024 };
void server(boost::asio::io_service& io_service, short port)
{
udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
udp::socket sock2(io_service, udp::endpoint(udp::v4(), 0));
for (;;)
{
char data[max_length];
udp::endpoint sender_endpoint;
size_t length = sock.receive_from(
boost::asio::buffer(data, max_length), sender_endpoint);
printf("Got data, sending response to %s:%d\n", sender_endpoint.address().to_string().c_str(), sender_endpoint.port());
sock2.send_to(boost::asio::buffer(data, length), sender_endpoint);
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_udp_echo_server <port>\n";
return 1;
}
boost::asio::io_service io_service;
using namespace std; // For atoi.
server(io_service, atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
我在您发布的代码中看不到任何错误。与您的[上一个问题](http://stackoverflow.com/questions/3571156/why-doesnt-this-boost-asio-code-work-with-this-python-client)类似,请使用异步UDP服务器示例,请发布您的客户代码。 – 2010-09-20 11:59:00
它是相同的,除非它不能在整个网络中工作!实际上从一台Linux PC到同一台PC上的一台Windows虚拟机。但其他代码的作品。 – Matt 2010-09-20 20:50:31