作者KuoZheChiou (LALALA)
看板C_and_CPP
標題[問題] sendto issue with ipv6/ipv4 on Windows
時間Thu Oct 22 10:47:24 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Windows, VC++
問題(Question):
由於開發網路程式要在IPv6和IPv4都能運作,
於是程式一開始會先創造一個AF_INET6的socket.
但是在Linux可以運作的程式, 在Windows上無法順利運作,
所以寫了一個簡易程式測試sendto
Linux測試程式
int main(void)
{
int sock_fd;
if((sock_fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
{
perror("socket failed");
return 0;
}
struct sockaddr_in remote;
int sizeremote = sizeof(struct sockaddr_in);
memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_port = htons(IP_PORT);
remote.sin_addr.s_addr = inet_addr(IP_Address);
char sendbuf[128] = "This is a test";
int rc = sendto(sock_fd, sendbuf, strlen(sendbuf), 0,
(struct sockaddr*)&remote, sizeremote);
if(rc < 0)
{
perror("sendto failed");
return 0;
}
return 1;
}
同樣的程式直接在VC2008上跑, 在sendto會回傳-1,
使用WSAGetLastErrore, 回傳為10014 (WSAEFAULT)
想請教, 能處理IPv6和IPv4的Windows程式, 理應當要如何撰寫?
(是否要先判斷IP version, 再開對應的socket? 不能像Linux?)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.219.195.33
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1445482046.A.111.html
推 longlongint: vs2008我不會 但是wiki IPv6 底下有 轉換機制的介紹 10/22 19:32
→ longlongint: 可以看 10/22 19:33
→ KuoZheChiou: 謝謝longlongint, 也看過了您提供的連結~ 10/23 09:37
→ KuoZheChiou: 網路上好像也有轉換的程式實作, 10/23 09:37
→ KuoZheChiou: 所以結論是, 要自己轉IP version的資料結構就是了? 10/23 09:38