我有執行下列步驟的程序:建立tcp連接時,什麼樣的地址給套接字?
客戶端:
- 發送一個特定的端口上UDP請求(多播)
- 檢查UDP響應
- 從所述響應中提取地址
- 設置套接字的地址和特定端口
服務器端:
- 接收並檢查一個特定的端口
- 上UDP請求(多播)發送具有內部IP地址UDP響應。
- 建立套接字的地址和特定端口
我的問題是它不與IPv6地址的工作:fe80:0000:0000:0000:0223:18ff:feed:ef59
.. 但它與IPv6的映射IPv4地址工作:0000:0000:0000:0000:0000:ffff:0a40:4caf
(對應於10.64.76.175
)
根據維基百科,10.xxx.xxx.xxx
ipv4地址是私有類A地址fe80::/10
是鏈接本地地址。 是我問題的根源嗎?
我得到這兩個地址從以太網接口:
ip addr
2 : enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:23:18:ed:ef:59 brd ff:ff:ff:ff:ff:ff inet 10.64.76.175/20 brd 10.64.79.255 scope global enp0s25 valid_lft forever preferred_lft forever inet6 fe80::223:18ff:feed:ef59/64 scope link valid_lft forever preferred_lft forever
更準確地說,問題來源於此摘錄:在socket()函數發送回錯誤。
int SetupTCPClientSocket(const char *host, const char *service) {
// Tell the system what kind(s) of address info we want
struct addrinfo addrCriteria; // Criteria for address match
memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
addrCriteria.ai_family = AF_UNSPEC; // v4 and v6 is OK
addrCriteria.ai_socktype = SOCK_STREAM; // Only streaming sockets
addrCriteria.ai_protocol = IPPROTO_TCP; // Only TCP protocol
// Get address(es)
struct addrinfo *servAddr; // Holder for returned list of server addrs
int rtnVal = getaddrinfo(host, service, &addrCriteria, &servAddr);
if (rtnVal != 0)
DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));
int sock = -1;
struct addrinfo *addr;
for (addr = servAddr; addr != NULL; addr = addr->ai_next)
{
// Create a reliable, stream socket using TCP
sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sock < 0)
{
continue; // Socket creation failed; try next address
}
// Establish the connection to the echo server
if (connect(sock, addr->ai_addr, addr->ai_addrlen) == 0)
{
break; // Socket connection succeeded; break and return socket
}
close(sock); // Socket connection failed; try next address
sock = -1;
}
freeaddrinfo(servAddr); // Free addrinfo allocated in getaddrinfo()
return sock;
}
我也許錯了,但你檢查什麼是存儲在你的結構addrinfo * addr;定期輸出? – user2310994