【发布时间】:2014-05-21 17:34:11
【问题描述】:
我有这两个 poco 程序,一个发送另一个接收。接收不会崩溃,但我无法让发送工作。可悲的是,异常也不是很有帮助,只是说“网络异常”。这可能与在同一台机器上运行两个程序有关,但重用地址仍然设置为 true,loopback 也是如此。
//Receive
#include <string>
#include <iostream>
// MulticastSocket receive example
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>
using namespace std;
int main(int argc, char* argv[])
{
Poco::Net::initializeNetwork();
std::cout << "1" << std::endl << std::flush;
//Poco::Net::SocketAddress localAddr(Poco::Net::IPAddress(), 1900);
Poco::Net::SocketAddress localAddr("239.255.255.250", 1900);
std::cout << "2" << std::endl << std::flush;
Poco::Net::MulticastSocket socket(localAddr, true);
std::cout << "3" << std::endl << std::flush;
socket.setLoopback(true);
std::cout << "4" << std::endl << std::flush;
socket.setTimeToLive(4);
std::cout << "5" << std::endl << std::flush;
Poco::Net::SocketAddress groupAddr("239.255.255.250", 1900);
std::cout << "6" << std::endl << std::flush;
socket.joinGroup(groupAddr.host());
std::cout << "7" << std::endl << std::flush;
Poco::Net::SocketAddress sender;
char buffer[512];
int n = socket.receiveFrom(buffer, sizeof(buffer), sender);
std::cout << buffer << std::endl << std::flush;
Poco::Net::uninitializeNetwork();
return 0;
}
//Send
#include <string>
#include <iostream>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>
#include <Poco/Net/NetException.h>
using namespace std;
using namespace Poco;
int main(int argc, char* argv[])
{
Poco::Net::initializeNetwork();
try
{
Poco::Net::SocketAddress address("239.255.255.250", 1900);
//Poco::Net::SocketAddress address(Poco::Net::IPAddress(), 1900);
std::cout << "1" << std::endl << std::flush;
Poco::Net::MulticastSocket socket(address, true);
std::cout << "2" << std::endl << std::flush;
socket.setLoopback(true);
std::cout << "3" << std::endl << std::flush;
//Poco::Net::SocketAddress sender;
char buffer[512];
buffer[0] = 'H';
buffer[1] = '\0';
std::cout << "Sending " << buffer << std::endl;
//socket.sendTo(buffer, 512, sender);
socket.sendBytes(buffer, 512, 0);
std::cout << "4" << std::endl << std::flush;
}
catch(const Poco::Net::NetException& ex)
{
std::cout << ex.what() << std::endl << std::flush;
}
Poco::Net::uninitializeNetwork();
return 0;
}
【问题讨论】:
-
[idf@localhost Debug]$ netstat -rn 内核 IP 路由表目标网关 Genmask 标志 MSS 窗口 irtt Iface 192.168.31.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 239.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0 0.0.0.0 192.168.31.2 0.0.0.0 UG 0 0 0 eth0 [idf@localhost 调试]$ ls
标签: c++ poco-libraries