我在使用 boost::asio::ip::udp 实现一个简单的 udp 客户端服务器时遇到了一个问题。如果我用 ipv6 链接范围单播地址绑定服务器,它在某些 PC 中有效,但在其他 PC 中无效个人电脑。
//main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <boost/asio.hpp>
#include <boost/format.hpp>
void test_udp_server(std::string const& host_, std::string const& port_)
{
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service);
boost::asio::ip::udp::resolver resolver(io_service);
boost::asio::ip::udp::endpoint endpoint= *resolver.resolve(boost::asio::ip::udp::resolver::query(host_, port_));
socket.open(endpoint.protocol());
std::cout << "address:" << endpoint.address().to_string() << " port:" << port_ << std::endl;
boost::system::error_code e;
socket.bind(endpoint, e);
if (e)
{
std::cerr << "bind error:" << e.message() << std::endl;
return;
}
//recieve from
boost::asio::ip::udp::endpoint remote_endpoint;
char buffer[4096];
unsigned int size;
while (1)
{
memset(buffer, 0, 4096);
size = 0;
size = socket.receive_from(boost::asio::buffer(buffer, 4096), remote_endpoint);
std::cout << "receive: " << std::string(buffer) << std::endl;
}
}
void test_udp_client(std::string const& host_, std::string const& port_)
{
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service);
boost::asio::ip::udp::resolver resolver(io_service);
boost::asio::ip::udp::endpoint dest_endpoint= *resolver.resolve(boost::asio::ip::udp::resolver::query(host_, port_));
socket.open(dest_endpoint.protocol());
int i = 0;
boost::system::error_code e;
while (i < 10)
{
std::string msg = boost::str(boost::format("udp message %d") % i);
socket.send_to(boost::asio::buffer(msg.c_str(), msg.size()), dest_endpoint, boost::asio::detail::message_do_not_route, e);
if (!e)
{
std::cout << "send: " << msg << std::endl;
}
else
{
std::cerr << "send error: " << e.message() << std::endl;
}
i ++;
}
}
int main(int ac, char* av[])
{
if (ac != 4)
{
std::cout << "ac != 4" << std::endl;
return -1;
}
if (std::string(av[1]) == "c")
{
test_udp_client(std::string(av[2]), std::string(av[3]));
}
else if (std::string(av[1]) == "s")
{
test_udp_server(std::string(av[2]), std::string(av[3]));
}
else
{
std::cout << "av[1] != \"c\" and av[1] != \"s\"" << std::endl;
return -1;
}
}
编译它,并像这样运行:
./main s fe80::211:85ff:fe6c:44ac%eth0 100 #server, "s" means server,
#"fe80::211:85ff:fe6c:44ac%eth0" means your ipv6 address
#and 100 is port
./main c fe80::211:85ff:fe6c:44ac 100 #run it in another ssh or shell
但结果是:
- 1 个 udp 服务器永远不会收到客户端发送的 msg
[root@wangsu Tmp2]# ls<br>
main main.cpp main.o Makefile<br>
[root@wangsu Tmp2]# ./main s fe80::211:85ff:fe6c:44ac%eth0 100<br>
address:fe80::211:85ff:fe6c:44ac%eth0 port:100<br>
2 udp 客户端,已成功发送消息
[root@wangsu Tmp2]# ./main c fe80::211:85ff:fe6c:44ac 100<br>
send: udp message 0<br>
send: udp message 1<br>
send: udp message 2<br>
send: udp message 3<br>
send: udp message 4<br>
send: udp message 5<br>
send: udp message 6<br>
send: udp message 7<br>
send: udp message 8<br>
send: udp message 9<br>
3 当我打开tcpdum时,它真的捕获了客户端发送的msg
[root@wangsu Tmp2]# tcpdump -i lo udp and host fe80::211:85ff:fe6c:44ac<br>
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br>
listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes<br>
10:55:41.215110 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215295 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215360 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215431 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215516 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215585 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215790 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.215926 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.216069 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
10:55:41.216130 IP6 fe80::211:85ff:fe6c:44ac.59007 > fe80::211:85ff:fe6c:44ac.100: UDP, length 13<br>
4这是我的ifconfig:
[root@wangsu Tmp2]# ifconfig<br>
eth0 Link encap:Ethernet HWaddr 00:11:85:6C:44:AC
inet addr:192.168.23.38 Bcast:192.168.23.255 Mask:255.255.255.0<br>
inet6 addr: fe80::211:85ff:fe6c:44ac/64 Scope:Link<br>
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1<br>
RX packets:1174940938 errors:0 dropped:0 overruns:0 frame:0<br>
TX packets:626463551 errors:0 dropped:0 overruns:0 carrier:0<br>
collisions:0 txqueuelen:100<br>
RX bytes:378565405153 (352.5 GiB) TX bytes:126427805747 (117.7 GiB)<br>
Base address:0x3400 Memory:fd000000-fd020000<br>
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0<br>
inet6 addr: ::1/128 Scope:Host<br>
inet6 addr: ::127.0.0.1/0 Scope:Compat<br>
UP LOOPBACK RUNNING MTU:16436 Metric:1<br>
RX packets:188781410 errors:0 dropped:0 overruns:0 frame:0<br>
TX packets:188781410 errors:0 dropped:0 overruns:0 carrier:0<br>
collisions:0 txqueuelen:0<br>
RX bytes:582726106596 (542.7 GiB) TX bytes:582726106596 (542.7 GiB)<br>
顺便说一句,在运行服务器和客户端之前,我已经停止了 iptables。更奇怪的是,在我的另一台电脑上它运行正常。
所以为什么?