2016-12-29 24 views
1

使用以下代碼我可以綁定到ipv4地址,但不綁定到也綁定到該地址的作用域全局ipv6地址機。我編譯這樣的代碼:無法使用Net :: DatagramSocket獲取poco 1.6.1或1.7.6綁定到ipv6

g++ -lPocoFoundation -lPocoXML -lPocoUtil -lPocoNet -lcrypto -lssl -I/usr/include/Poco -o pocoudpipv6 pocoudpipv6.cpp

當我執行./pocoudpipv6 10.X.X.X,它擁有打開插座和週期的「請稍候」,直到我打CTRL-C,這是預期。臺報告插座:

# ss -nelup |grep 20000 UNCONN 0 0 10.X.X.X:20000 *:* users:(("pocoudpipv6",pid=2444,fd=3)) uid:1000 ino:14526705 sk:2a <->

但是,當我與./pocoudpipv6 2001:X:X:X::X:X執行,出現這種情況:

我們從:: 1到2001重置ip地址:X:X:X :: X: X 地址族是ipv6 啓動失敗。錯誤是網絡例外:不支持地址系列

在Slackware64 14.2以及在Debian 8 jessie amd64上的1.6.1上發生此問題。據我讀過,在Poco中啓用ipv6應該是默認的。爲了讓這個測試用例能夠使用ipv6,還有什麼我需要做的嗎?

而且我至少有一個守護程序綁定到IPv6套接字這臺機器上:

udp UNCONN 0 0 2001:X:X:X::X:X:123 :::* users:(("ntpd",pid=2133,fd=22)) ino:5247 sk:19 v6only:1 <->

提前感謝!

代碼如下:

#include <iostream> 
#include <Net/DatagramSocket.h> 
#include <Net/SocketAddress.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <signal.h> 

struct sigaction sigact = { 0 }; 

struct io_handling { 
     uint8_t exit_value; 
}; 

struct io_handling io_handler = { 0 }; 

static void sigint_signal_handler(int sig) 
{ 
    if(sig == SIGINT) { 
     std::cout << std::endl << "Commencing shutdown in 5... 4... 3... 2... 1..." << std::endl; 
     io_handler.exit_value = 1; 
    } 
} 

static void cleanup(void) 
{ 
    sigemptyset(&sigact.sa_mask); 
} 

int main(int argc, char **argv) 
{ 
    Poco::Net::DatagramSocket *pSocket = new Poco::Net::DatagramSocket(); 
    Poco::UInt16 port = 20000; 
    Poco::Net::IPAddress *ipAddress = new Poco::Net::IPAddress("::1"); 

    if(argc == 2) { 
     delete ipAddress; 
     ipAddress = new Poco::Net::IPAddress(argv[1]); 
     std::cout << std::endl << "We're resetting the ipaddress from ::1 to " << argv[1]; 
    } 

    std::cout << std::endl << "Address family is "; 

    if(ipAddress->family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv6)) { 
     std::cout << "ipv6 "; 
    } else if(ipAddress->family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv4)) { 
     std::cout << "ipv4 "; 
    } else { 
     std::cout << "something else, something very wrong."; 
    } 

    try { 
     sigact.sa_handler = sigint_signal_handler; 
     sigemptyset(&sigact.sa_mask); 
     sigact.sa_flags = 0; 
     sigaction(SIGINT, &sigact, (struct sigaction *)NULL); 

     pSocket->bind(Poco::Net::SocketAddress(*ipAddress, port)); 
     while(!io_handler.exit_value) { 
      sleep(1); 
      std::cout << std::endl << "Waiting..."; 
     } 
    } catch(Poco::Exception& ex) { 
     std::cout << std::endl << "Failure launching. Error was " << ex.displayText() << std::endl; 
    } 

    delete pSocket; 
    delete ipAddress; 

    return 0; 
} 
+0

自解決。我正在使用的lib只調用Poco :: Net :: DatagramSocket的默認ctor,然後給出一個僅用於ipv4的套接字。調用:: bind()不會根據輸入的IP地址重置。 –

回答

0

自行解決。我正在使用的lib只調用Poco :: Net :: DatagramSocket的默認ctor,然後給出一個僅用於ipv4的套接字。調用:: bind()不會根據輸入的IP地址重置。

解決方法是在上面的例子中處理ipAddress之前不是新的pSocket,然後將該端口作爲Poco :: Net :: SocketAddress傳遞給ctor,它將創建合適的套接字類型並自動綁定它。

相關問題